“assignment discards 'const' qualifier” error on non-const pointer

后端 未结 3 1312
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-19 01:47

In the following function:

char *mystrtok(const char *input, const char *delim,char *rest) {
    int i;
    for (i = 0; input[i] != *delim && input[i         


        
相关标签:
3条回答
  • 2021-01-19 02:16

    change the prototype to

    char *mystrtok(const char *input, const char *delim, const char *rest);
    
    0 讨论(0)
  • 2021-01-19 02:17

    you could also cast your 'input' variable with a (char*) type which would resolve the warning. just be careful using explicit casts like this so as not to modify constants themselves.

    rest = (char*)input + i + 2;
    
    0 讨论(0)
  • 2021-01-19 02:24

    input is a pointer to a constant char, and you're assigning it to a pointer to a non-constant char. This here might be an interesting reading for you.

    0 讨论(0)
提交回复
热议问题