目的很单纯的一个读取表格格式的数字文本文件并存入二维数组的程序,编译总说类型不匹配!

fangzy_2005 2009-02-27 11:15:33
目的很单纯的一个读取table格式的数字文件并存入二维数组的程序,但是编译总说main函数中的table[10][20]类型不对,觉得很纳闷,看上去和声明中的类型明明一模一样啊?
困惑难解,请大牛试着运行一下,如知道原因所在,还望不吝赐教,万分感谢!

#include <stdio.h>

// suppose we know the dimension of the data table in advance
double data[10][20];
int readtab_v1(FILE *, char, double table[][20], long, long);

main()
{
FILE *fp;

fp = fopen("prop.table","r");
printf("table element:\n");
readtab_v1(fp, '\t', data[10][20], 10, 20);
printf("%lf\n",data[1][3]);

}


//int readtab_v1(FILE *fp, char sep, double table[10][20], long nrow, long ncol)
int readtab_v1(FILE *fp, char sep, double table[][20], long nrow, long ncol)
{
int i,j;

if (sep!='\t')
return 1;

for(i=0;i<nrow;i++){
for(j=0;j<ncol;j++)
fscanf(fp, "%lf\t", &table[i][j]);
j=0;
}
return 0;
}
...全文
74 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangzy_2005 2009-02-27
  • 打赏
  • 举报
回复
果然可以了,非常感谢,但是为什么在K&R书上却明明说如下三种传递方式都是可以的啊?
f(int daytab[2][13]) { ... }
f(int daytab[][13]) { ... }
f(int (*daytab)[13]) { ... }


附书上原文如下:
If a two-dimensional array is to be passed to a function, the parameter declaration in the
function must include the number of columns; the number of rows is irrelevant, since what is
passed is, as before, a pointer to an array of rows, where each row is an array of 13 ints. In
this particular case, it is a pointer to objects that are arrays of 13 ints. Thus if the array
daytab is to be passed to a function f, the declaration of f would be:
f(int daytab[2][13]) { ... }
It could also be
f(int daytab[][13]) { ... }
since the number of rows is irrelevant, or it could be
f(int (*daytab)[13]) { ... }
which says that the parameter is a pointer to an array of 13 integers. The parentheses are
necessary since brackets [] have higher precedence than *. Without parentheses, the
declaration
int *daytab[13]
is an array of 13 pointers to integers. More generally, only the first dimension (subscript) of an
array is free; all the others have to be specified.
yangkunhenry 2009-02-27
  • 打赏
  • 举报
回复
readtab_v1(fp, '\t', data, 10, 20);

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧