[問題] 陣列指標與陣列: 位址相同, 值卻不同

看板C_and_CPP作者 (風之子)時間14年前 (2010/04/07 00:02), 編輯推噓2(201)
留言3則, 2人參與, 最新討論串1/5 (看更多)
各位大大 想請教一個觀念 在下面的情況下, a+2 與 ptr+1 的 位址是相同的 但是為什麼*(a+2) 與 *(ptr+1) 的 值不同呢 想了好久 (我是在gcc 4.3下面編譯) #include<stdio.h> int main(int argc, char* argv[]) { int a[5]={1,2,3,4,5}; int (*ptr)[2]=a; printf("%p\n", a+2); //0xbf9cdd54 printf("%d\n", *(a+2)); //3 printf("%p\n", ptr+1); //0xbf9cdd54 printf("%d\n",*(ptr+1)); //-1080238764 } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.32.4.97 ※ 編輯: inventorr 來自: 114.32.4.97 (04/07 00:05)

04/07 02:46, , 1F
int (*ptr)[2]=a; <= 這個編譯器應該會不給編吧@@
04/07 02:46, 1F

04/07 02:47, , 2F
改成int *ptr=a;
04/07 02:47, 2F

04/07 10:29, , 3F
試比較 1. int a[5]跟int* p 2.int a[3][2]跟int(*p)[2]
04/07 10:29, 3F
文章代碼(AID): #1BkriYQN (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1BkriYQN (C_and_CPP)