使用const保护二维数组内容,出现警告
在《C Primer Plus》第十章(数组和指针)中,关于数据保护,提到使用const保护数据。下面是我自己的练习:
程序代码:#include <stdio.h>
void show(const int array[][10]); //使用const保护数组内容,出现警告;取消const之后,警告消失
void display(const int *);
int main(void)
{
int array[10][10], integer[100];
int i, j, count=0, index;
for(i=0; i<10; i++)
for(j=0; j<10; j++, count++){
array[i][j] = count+1;
integer[count] = count+1;
}
show(array);
display(integer);
return 0;
}
void show(const int array[][10]) //使用const保护数组内容,出现警告;取消const之后,警告消失
{
int i, j;
for(i=0; i<10; i++){
for(j=0; j<10; j++)
printf("%3d ", array[i][j]);
putchar('\n');
}
}
void display(const int integer[])
{
int index;
for(index=0; index<100; index++)
printf("%3d ", integer[index]);
putchar('\n');
}
array.c: In function 'main':array.c:16: warning: passing argument 1 of 'show' from incompatible pointer type
array.c:3: note: expected 'const int (*)[10]' but argument is of type 'int (*)[10]'
保护一维数组内容不会有任何问题,但是保护二维数组内容则出现警告,对二位数组取消内容保护,则警告消失。
两种使用方法的差异在哪里?
为什么?
如何保护二维(多维)数组的内容?
谢谢。








