二维数组
#include
void cp(int h,int g,const double sar[*][*],double lar[*][*]);//复制变长二维数组
void pri(int a,int b,const double [*][*]);//打印变长二维数组
int main(void)
{
int k,l;
double ar[3][5];
for(k=0;k<3;k++)
{
for(l=0;l<5;l++)
ar[k][l]=k*l/3; //初始化数组ar
}
double ar2[3][5];
cp(3,5,ar,ar2);
printf("ar数组\n");
pri(3,5,ar);
printf("ar2数组\n");
pri(3,5,ar2);
}
void cp(int h,int g,const double sar[h][g],double lar[h][g])
{
int j,k;
for(j=0;j
{
for(k=0;k
lar[j][k]=sar[j][k];
}
}
void pri(int a,int b,const double ar[a][b])
{
int k,i;
for(k=0;k
{
for(i=0;i
printf("%d*%d=%.2f",k,i,ar[k][i]);
putchar('\n');
}
}
可以编译成功,但是可执行文件无法执行,为什么。编译有3个警告无错误。
警告如下:
10.13.8.c: In function ‘main’:
10.13.8.c:14:2: warning: passing argument 3 of ‘cp’ from incompatible pointer type [enabled by default]
cp(3,5,ar,ar2);
^
10.13.8.c:2:6: note: expected ‘const double (*)[]’ but argument is of type ‘double (*)[5]’
void cp(int h,int g,const double sar[*][*],double lar[*][*]);//复制变长二维数组
^
10.13.8.c:16:2: warning: passing argument 3 of ‘pri’ from incompatible pointer type [enabled by default]
pri(3,5,ar);
^
10.13.8.c:3:6: note: expected ‘const double (*)[]’ but argument is of type ‘double (*)[5]’
void pri(int a,int b,const double [*][*]);//打印变长二维数组
^
10.13.8.c:18:2: warning: passing argument 3 of ‘pri’ from incompatible pointer type [enabled by default]
pri(3,5,ar2);
^
10.13.8.c:3:6: note: expected ‘const double (*)[]’ but argument is of type ‘double (*)[5]’
void pri(int a,int b,const double [*][*]);//打印变长二维数组
^
下面是截图:
------解决方案--------------------
void cp(int h,int g,const double sar[h][g],double lar[h][g])
{
int j,k;
for(j=0;j
{
for(k=0;k
lar[j][k]=sar[j][k];
}
}
------解决方案--------------------
警告说的是cp的第二个参数期望的是conts double (*)[]类型,但是实际传入的参数是double (*)[5]类型的,这个可以不用理会。
第一个函数没有输出,还有就是k为int型,k*1/3的结果肯定是int型。最后赋值只是把int型转换为了double。
其他的暂时没有看出问题。