[問題] c 陣列與指標

看板C_and_CPP作者時間8年前 (2016/02/03 21:53), 8年前編輯推噓2(205)
留言7則, 5人參與, 最新討論串1/1
I have met some questions about pointer in C. int main() { int a[5][2] = {0,1,2,3,4,5,6,7,8,9}; int *p = a[0]; int (*p2)[2] = &a[1]; ++p; ++p2; // 1, a[0][1] printf("%d\n",*p); // 4, a[2][0] printf("%d\n",**p2); // 9, but I think it would run out of index. (2 > 1) -> error printf("%d \n",p2[1][2]); return 0; } Could anyone give me some comments about it. BTW, How to get 5 using pointer p2? Can *(*p2+1) get 5 ? Thanks. -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.229.141.7 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1454507637.A.887.html ※ 編輯: NoStra (36.229.141.7), 02/03/2016 21:56:44

02/03 22:06, , 1F
int (*p2)[2] = &a[1]; 我看不懂這一行在做什麼T.T
02/03 22:06, 1F

02/03 23:50, , 2F
just treat p2[1][2] as *(*p2 + 2 * 1 + 2)
02/03 23:50, 2F

02/03 23:51, , 3F
*(*p2 + 1) = 5 is correct.
02/03 23:51, 3F

02/04 13:37, , 4F
理解就好,別用這種寫法搞自己搞別人,就用p[r][c]來存取
02/04 13:37, 4F

02/04 16:03, , 5F
*p2 is an array of int *(*p2 + 3) = 5
02/04 16:03, 5F

02/04 16:59, , 6F
it's your job to check the index bounds
02/04 16:59, 6F

02/04 17:00, , 7F
this is not Java lol
02/04 17:00, 7F
文章代碼(AID): #1MiWPrY7 (C_and_CPP)