c++ - 2 dimensional array in pointers -
#include<stdio.h> void main() { int a[] = { 1, 2, 3, 4 }; int b[] = { 5, 6, 7 }; int *p[2]; p[0] = a; p[1] = &b + 1; printf("%d\n%d", &p[1][0], p[0][1]); }
here p 1d array of pointers, how come 2d array used in printf statement. output 1 2.
the subscript operator [] defined pointer expressions array expressions (which implicitly converted pointer expressions before subscript applied).
the look a[i] evaluated *(a + i); a pointer, , a + i gives address of i'th element of array (pointer arithmetic based on pointed-to type; if a points int, a + i gives address of i'th int next 1 pointed a).
so given array of pointers:
int *p[2]; the look p[0] has pointer type, can add together offset , dereference result: *(p[0] + 1), same writing p[0][1].
c++ c pointers
No comments:
Post a Comment