指針問題我有以下的C代碼:與C的相同指針不同的事情
int main(int argc, char *argv[])
{
int n = argc - 1;
int array[n];
int m[n][n];
int i = 0;
for(i = 1; i<=n;i++)
{
array[i] = atoi(argv[i]);
printf("%d\n",array[i]);
}
printf("array[4] = %d\n",array[4]);
for(i = 1; i<=n;i++)
{
m[i][i] = 0;
printf("address of m[i][i] = %p\n",&m[i][i]);
}
printf("value of array[4] =%d pointer = %p\n",array[4],&array[4]);
for(i=1;i<=n;i++) printf("After %d\n",array[i]);
return 0;
}
如果我用下面的命令運行:」 ./program 30 35 15 5 10 20 15" 的輸出是:
30
35
15
5
10
20
25
array[4] = 5
address of m[i][i] = 0xbf93070c
address of m[i][i] = 0xbf93072c
address of m[i][i] = 0xbf93074c
address of m[i][i] = 0xbf93076c
address of m[i][i] = 0xbf93078c
address of m[i][i] = 0xbf9307ac
address of m[i][i] = 0xbf9307cc
value of array[4] =0 pointer = 0xbf9307cc
After 30
After 35
After 15
After 0
After 10
After 20
After 25
注意數組[4]與m [n] [n]具有相同的指針。我真的不明白這是如何可能的。代碼有什麼問題。爲什麼array [4] = m [n] [n]?
你意識到你的數組索引超出了範圍嗎? – 2013-02-11 00:35:24