ycs*_*hao 2 c pointers const function
为什么这段代码会给我一个警告:从不兼容的指针类型传递"test"的参数1?我知道这是关于char之前的const,但为什么呢?
void test(const int ** a)
{
}
int main()
{
int a=0;
int *b=&a;
int **c=&b;
test(c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
你不能分配int **
给a const int **
,因为如果你这样做,后一个指针将允许你给一个int *
变量一个const int
对象的地址:
const int myconst = 10;
int *intptr;
const int **x = &intptr; /* This is the implicit conversion that isn't allowed */
*x = &myconst; /* Allowed because both *x and &myconst are const int * ... */
/* ... but now intptr points at myconst, and you could try to modify myconst through it */
Run Code Online (Sandbox Code Playgroud)