c语言中const参数莫名警告(编译器版本问题)
001、
[root@PC1 test1]# ls test.c [root@PC1 test1]# cat test.c ## 测试c程序 #include <stdio.h> void print_array(const int x[4][3]); // 函数原型声明 int main(void) { int a[4][3] = {{1,2,4},{2,1,4},{2,5,1},{4,2,3}}; print_array(a); //此处调用二维数组 return 0; } void print_array(const int x[4][3]) //定义函数的时候使用const关键词, 因为只需要显示数组的元素,而不修改数组的元素 { int i,j; for(i = 0; i < 4; i++) { for(j = 0; j < 3; j++) { printf("value = %d ", x[i][j]); } putchar('\n'); } } [root@PC1 test1]# gcc test.c -o kkk ## 编译过程中遇到警告, 这个警告为什么会产生? test.c: In function ‘main’: test.c:8:2: warning: passing argument 1 of ‘print_array’ from incompatible pointer type [enabled by default] print_array(a); ^ test.c:3:6: note: expected ‘const int (*)[3]’ but argument is of type ‘int (*)[3]’ void print_array(const int x[4][3]); ^ [root@PC1 test1]# ls kkk test.c [root@PC1 test1]# ./kkk ## 运算 value = 1 value = 2 value = 4 value = 2 value = 1 value = 4 value = 2 value = 5 value = 1 value = 4 value = 2 value = 3
。
002、去除const关键词(和上一个程序一致,只是去除了关键词const)
[root@PC1 test1]# ls test.c [root@PC1 test1]# cat test.c ## 测试程序 #include <stdio.h> void print_array(int x[4][3]); // 函数原型声明 int main(void) { int a[4][3] = {{1,2,4},{2,1,4},{2,5,1},{4,2,3}}; print_array(a); // 调用二维数组参数,只需要给数组名 return 0; } void print_array(int x[4][3]) // 函数定义的时候不适用关键词const { int i,j; for(i = 0; i < 4; i++) { for(j = 0; j < 3; j++) { printf("value = %d ", x[i][j]); } putchar('\n'); } } [root@PC1 test1]# gcc test.c -o kkk ## 编译过程中没有警告信息 [root@PC1 test1]# ls kkk test.c [root@PC1 test1]# ./kkk ## 运算测试 value = 1 value = 2 value = 4 value = 2 value = 1 value = 4 value = 2 value = 5 value = 1 value = 4 value = 2 value = 3
。
验证:
a、
[root@PC1 test]# ls test.c [root@PC1 test]# gcc --version ## 查看一下编译器版本 gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36) Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [root@PC1 test]# cat test.c ## 测试程序 #include <stdio.h> void print_array(const int a[3][2]); int main(void) { int i,j; int a[3][2] = {{4,3},{2,3},{1,4}}; print_array(a); return 0; } void print_array(const int a[3][2]) { int i,j; for(i = 0; i < 3; i++) { for(j = 0; j < 2; j++) { printf("%4d", a[i][j]); } puts(""); } } [root@PC1 test]# gcc test.c -o kkk ## 编译,出现警告 test.c: In function ‘main’: test.c:7:9: warning: passing argument 1 of ‘print_array’ from incompatible pointer type [enabled by default] print_array(a); ^ test.c:2:6: note: expected ‘const int (*)[2]’ but argument is of type ‘int (*)[2]’ void print_array(const int a[3][2]); ^ [root@PC1 test]# ls kkk test.c
。
b、编译器有4.8切换到11.4
[root@PC1 test]# ls test.c [root@PC1 test]# gcc --version ## 编译器版本 gcc (GCC) 11.4.1 20231218 (Red Hat 11.4.1-3) Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [root@PC1 test]# cat test.c ## 测试程序(和上一个一样) #include <stdio.h> void print_array(const int a[3][2]); int main(void) { int i,j; int a[3][2] = {{4,3},{2,3},{1,4}}; print_array(a); return 0; } void print_array(const int a[3][2]) { int i,j; for(i = 0; i < 3; i++) { for(j = 0; j < 2; j++) { printf("%4d", a[i][j]); } puts(""); } } [root@PC1 test]# gcc test.c -o kkk ## 编译 [root@PC1 test]# ls kkk test.c
。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2023-11-19 ./SNeP_111: /lib64/libm.so.6: version `GLIBC_2.29' not found (required by ./SNeP_111)
2023-11-19 exonerate-软件的安装
2022-11-19 ModuleNotFoundError: No module named 'gffutils'
2022-11-19 ModuleNotFoundError: No module named 'Bio'
2021-11-19 python3中提取包含特定字符的行
2021-11-19 shell 和 R 实现具有映射关系的数据的批量替换
2021-11-19 R语言中gsub使用示例记录