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

 。

 

posted @ 2024-11-19 22:30  小鲨鱼2018  阅读(6)  评论(0编辑  收藏  举报