我有一個不尋常的情況。 這裏的片段:不正確的2D陣列訪問
int i, j;
short ** s = (short **)malloc(128);
for(i = 0; i < 14; i++){
s[i] = (short *)malloc(128);
for(j = 0; j < 128; j++)
s[i][j] = 0;
}
printf("Value of s[%d][%d] = %d\n",2,40,s[2][40]);
s[1][108] = 99;
printf("Value of s[%d][%d] = %d\n",2,40,s[2][40]);
輸出我得到當我運行是這樣的:Value of s[2][40] = 0 Value of s[2][40] = 99
消除環路,寫短S [14] [128]產生正確的輸出(S的值[2 ] [40]在這兩個打印中都爲0)
爲什麼我能用s [1] [108]訪問s [2] [40]? 我在Ubuntu 12.04上使用gcc。
OT:即使你有一個語句一個循環,你應該始終把括號(好習慣) – 2014-09-03 11:26:30
[請不要投的返回值'C中的malloc()'](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc)。 – unwind 2014-09-03 11:29:44