passing arg 1 of `foo' from incompatible pointer type

后端 未结 3 998
囚心锁ツ
囚心锁ツ 2021-01-26 10:11

Why this shows warning:

#include
foo (const char **p)
{ 

}

int main(int argc , char **argv)
{
    foo(argv);
}

But following

3条回答
  •  终归单人心
    2021-01-26 10:56

    In the first version you are casting between two different types of pointer not simply adding a const to the pointer.

    • char ** is a pointer to a (pointer to a char)
    • const char ** is a pointer to a (pointer to a const char)

    As you can see these pointer point to different types similar to the more obviously questionable:

    int *i;
    double *d;
    d = i;
    

    In your second example you see that you can cast from a pointer to a const pointer so if you were to apply this to your situation you would need to have a const pointer to (a pointer to a char).

    foo(char * const *p);
    

提交回复
热议问题