主题:二维数组,传递指针,类型不兼容
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*编程练习10-8,初始化一个3 * 5的二位数组,并利用一个基于变长数组的函数把
*该数组复制到另一个一个二位数组;还要编写一个基于变长数组的函数来显示两个
*数组的内容(该函数应该能够处理任意的N * M数组)
**/
#include <stdio.h>
void copy(int m, int n, const double array[m][n], double target[m][n]);
void show(int m, int n, const double *array[]);
int main(void)
{
int m=3, n=5;
double array[3][5] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15}
};
double target[m][n];
copy(m, n, array, target);
show(m, n, target);
return 0;
}
void copy(int n, int m, const double array[m][n], double target[m][n])
{
int i, j;
for(i=0; i<m; i++)
for(j=0; j<n; j++)
target[m][n] = array[m][n];
}
void show(int m, int n, const double *array[])
{
int i, j;
for(i=0; i<m; i++){
for(j=0; j<n; j++)
printf("%f ", array[m][n]);
putchar('\n');
}
printf("显示完毕。\n");
}
10-8.c: 在函数‘main’中:
10-8.c:21:5: 警告: 传递‘copy’的第 3 个参数时在不兼容的指针类型间转换 [默认启用]10-8.c:8:6: 附注: 需要类型‘const double (*)[(sizetype)(n)]’,但实参的类型为‘double (*)[5]’
10-8.c:22:5: 警告: 传递‘show’的第 3 个参数时在不兼容的指针类型间转换 [默认启用]
10-8.c:9:6: 附注: 需要类型‘const double **’,但实参的类型为‘double (*)[(sizetype)(n)]’

您所在位置: