为什么下面的代码会发出警告?
#include <stdio.h>
int main()
{
int arr[3] = {1,2,3};
int *ptr, *ptr1;
ptr = &arr;
ptr1 = arr;
printf("ptr is %p\t ptr1 is %p\n",ptr, ptr1);
ptr++;
ptr1++;
printf("ptr is %p\t ptr1 is %p\n",ptr, ptr1);
return 0;
}
$ gcc test.c -o test
test.c: In function ‘main’:
test.c:6:6: warning: assignment from incompatible pointer type [enabled by default]
ptr = &arr;
我也尝试更改 ptr
至int **ptr
,但它仍然失败并出现相同的警告。 arr
是一个指针;我将它存储在指针中。这里有什么错误吗?
最佳答案
arr is a pointer
不,不是。
MSVC(令人惊讶地)对此代码给出了更详细的警告:
warning C4047: '=': 'int *' differs in levels of indirection from 'int (*)[3]'
不带下标的语法&array
包含长度信息。将分配更改为 &arr[0]
会删除警告并按预期工作。
关于c - 数组上不兼容指针类型警告的分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54310733/