[問題] c 陣列與指標

作者: NoStra   2016-02-03 21:53:54
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.
作者: stupid0319 (徵女友)   2016-02-03 22:06:00
int (*p2)[2] = &a[1]; 我看不懂這一行在做什麼T.T
作者: arthur104 (arthur)   2016-02-03 23:50:00
just treat p2[1][2] as *(*p2 + 2 * 1 + 2)*(*p2 + 1) = 5 is correct.
作者: qscgy4 (有點厲害)   2016-02-04 13:37:00
理解就好,別用這種寫法搞自己搞別人,就用p[r][c]來存取
作者: CoNsTaR ((const *))   2016-02-04 16:03:00
*p2 is an array of int *(*p2 + 3) = 5
作者: Frozenmouse (*冰之鼠*)   2016-02-04 16:59:00
it's your job to check the index boundsthis is not Java lol

Links booklink

Contact Us: admin [ a t ] ucptt.com