【发布时间】:2016-03-08 05:50:31
【问题描述】:
如果我将二维数组定义为:
char *tablechar[][2] = {
{"0", "Innova"},
{"2", "Brio" },
{"3", "Alto" },
{"4", "Bolero"},
{"5", "Swift" }
};
和代码在同一个文件中:
printf("Value = %d\n",sizeof(tablechar));
printf("Value = %d\n",sizeof(tablechar[0]));
printf("Num of rows = %d\n",sizeof(tablechar)/sizeof(tablechar[0]));
输出如预期:
Value = 40
Value = 8
Num of rows = 5
现在,我将这个二维矩阵传递给一个函数,其定义为:
void printstuff(char *tab[][2])
{
printf("Value = %d\n",sizeof(tab));
printf("Value = %d\n",sizeof(tab[0]));
printf("Num of rows = %d\n",sizeof(tab)/sizeof(tab[0]));
}
我得到以下输出:
Value = 4
Value = 8
Num of rows = 0
在上述函数中带有编译器警告:
为什么这样sizeof(tab)和sizeof(tablechar)的值不同?
【问题讨论】:
-
4 是指针的大小。
标签: c arrays string multidimensional-array