Pointer to a 2d array

I have a question about a pointer to a 2d array. If the array looks like

int a[2][3]; 

then is it a pointer to array a ?

 int (*p)[3] = a; 

If this is correct, I wonder what [3] means from int(*p)[3] ?

+48
c ++ arrays pointers multidimensional-array
Dec 23 '11 at 15:02
source share
6 answers

Instead of referring to int[2][3] as a "2d array", you should consider it an "array of arrays". This is an array with two elements in it, where each element itself is an array with 3 ints in it.

 int (*p)[3] = a; 

You can use p to point to either of the two elements in a . p points to an array with three goals, namely, the first such element. p+1 will point to a second array with three goals. To initialize p to point to the second element, use:

 int (*p)[3] = &(a[1]); 

The following are equivalent ways of pointing to the first of two elements.

 int (*p)[3] = a; // as before int (*p)[3] = &(a[0]); 
+36
Dec 23 '11 at 3:19
source share
 int a[2][3]; 

a read as array 2 of array 3 of int, which is simply an array of arrays. When you write

int (*p)[3] = a;

It declares p as a pointer to the first element, which is an array. Thus, p points to an array of 3 ints, which is an element of an array of arrays.

Consider the following example:

  int a[2][3] +----+----+----+----+----+----+ | | | | | | | +----+----+----+----+----+----+ \_____________/ | | | p int (*p)[3] 

Here p is your pointer, which points to an array of 3 ints, which is an element of an array of arrays.

+37
Dec 23 '11 at 3:17
source share

Strictly speaking, no, int (*p)[3] = a; not a pointer to a . This is a pointer to the first element of a . The first element a is an array of three integers. p is a pointer to an array of three integers.

A pointer to array a will be declared this way:

 int (*q)[2][3] = &a; 

The numerical values ​​of p and q most likely (or perhaps should even be) the same, but they are of different types. This will work when you do arithmetic on p or q . p+1 points to the second element of array a , and q+1 points to memory immediately after the end of array a .

Remember: cdecl is your friend: int a[2][3] , int (*q)[2][3] .

+8
Dec 23 '11 at 3:12
source share

[3] is part of the type. In this case, p is a pointer to an array of size 3 that contains ints.

A particular array type always includes its size, so you have int *[3] or int *[5] types, but not just int *[] , which has undefined size.

 int *x[20]; /* type of x is int *[20], not just int *[] */ int y[10][10]; /* type of y is int[10][10], not just int[][] */ 
+5
Dec 23 '11 at 15:06
source share

Also note:

 int *p[5] // p is an array of 5 pointers int (*p)[5] // p points to an array of 5 ints int (*(p+5))[10] // p is a pointer to a structure where the structure 5th element has 10 ints . 
+2
Oct 25 '15 at 20:04
source share

You can point to a 2d array as a 1d array

 #include <iostream> int main() { int array[2][2] = {{0,1}, {2,3}}; // array int *ptr; ptr=(int*)array; std::cout << *(ptr) << '\n';//out 0 std::cout << *(ptr+1) << '\n';//out 1 std::cout << *(ptr+2) << '\n';//out 2 std::cout << *(ptr+3) << '\n';//out 3 } 
0
Feb 05 '19 at 7:30
source share



All Articles