如果我定義的2D陣列如:sizeof運算
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
現在,我將這個2D矩陣傳遞給一個函數,其定義如下:
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的值(標籤)和sizeof(tablechar)不同?
4是指針的大小。 –
[如何找到'sizeof'(一個指向數組的指針)]]的可能的副本(http://stackoverflow.com/questions/492384/how-to-find-the-sizeofa-pointer-pointing-to -an-array) –
[C編程語言中數組的大小?](https://stackoverflow.com/questions/1975128/sizeof-an-array-in-the-c-programming-language?) – kaylum