warning: assignment discards qualifiers from pointer target type

后端 未结 2 786
花落未央
花落未央 2020-12-05 09:56

I wrote the following code:

void buildArrays(char *pLastLetter[],int length[], int size, const char str[]) {

    int i;
    int strIndex = 0;
    int letter         


        
相关标签:
2条回答
  • 2020-12-05 10:23

    Well, as you said yourself, pLastLetter is an array of char * pointers, while str is an array of const char. The &str[strIndex-1] expression has type const char*. You are not allowed to assign a const char* value to a char * pointer. That would violate the rules of const-correctness. In fact, what you are doing is an error in C. C compilers traditionally report it as a mere "warning" to avoid breaking some old legacy code.

    As for "how to fix it"... It depends on what you are trying to do. Either make pLastLetter an array of const char* or remove the const from str.

    0 讨论(0)
  • 2020-12-05 10:27

    str is const, pLastLetter isn't. It's saying the const qualifier is discarded if you do this.

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