strtok wont accept: char *str

后端 未结 3 1697
名媛妹妹
名媛妹妹 2020-12-10 19:40

strtok wont work correctly when using char *str as the first parameter (not the delimiters string).

Does it have something to do with the area that allocates strings

3条回答
  •  臣服心动
    2020-12-10 19:59

    In the first case, you pass a string literal to strtok(). As strtok() modifies this string, and as string literals cannot legally be modified, you end up with undefined behaviour. In the second case, the compiler copies the string into the array. Array contents can be modified, so this code is OK.

提交回复
热议问题