小编Jul*_*rry的帖子

不兼容的指针类型和常量

我有一个函数采用静态二维数组并将数组元素的元素视为常量:

void test_function(const char arr[3][3]);
Run Code Online (Sandbox Code Playgroud)

我试图调用如下函数:

char my_var[3][3] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} };
test_function(my_var);
Run Code Online (Sandbox Code Playgroud)

使用gcc编译(没有任何标志)时,我收到以下警告:

test.c:9:8: warning: passing argument 1 of 'test_function' from incompatible pointer type
   test_function(my_var);
                 ^
test.c:4:6: note: expected 'const char (*)[3]' but argument is of type 'char (*)[3]'
 void test_function(const char arr[3][3]);
Run Code Online (Sandbox Code Playgroud)

如果我删除了consttest_function的原型,警告消失.但它并不是我想要的.

用clang编译两者时-pedantic-errors,-Wall我没有得到任何关于指针不兼容的警告.

我只是想了解为什么gcc会在这种情况下输出这样的警告.为什么我的指针/数组不兼容?

c pointers

17
推荐指数
2
解决办法
2057
查看次数

标签 统计

c ×1

pointers ×1