Problem with processing individual strings stored in an array of pointers to multiple strings in C

前端 未结 3 1373
無奈伤痛
無奈伤痛 2020-12-22 07:30

An array of pointers to strings is provided as the input. The task is to reverse each string stored in the input array of pointers. I\'ve made a function called reverseStrin

3条回答
  •  失恋的感觉
    2020-12-22 07:39

    You are trying to change string literals.

    String literals are usually not modifiable, and really should be declared as const.

    const char *s[] = {"abcde", "12345", "65gb"};
    /* pointers to string literals */
    

    If you want to make an array of modifiable strings, try this:

    char s[][24] = {"abcde", "12345", "65gb"};
    /* non-readonly array initialized from string literals */
    

    The compiler will automatically determine you need 3 strings, but it can't determine how long each needs to be. I've made them 24 bytes long.

提交回复
热议问题