# A pointer to 2d array

I have a question about a pointer to 2d array. If an array is something like

```int a;
```

then, is this a pointer to array a?

```int (*p) = a;
```

If this is correct, I am wondering what does  mean from int(*p)?

Rather than referring to int as a '2d array', you should consider it to be an 'array of arrays'. It is an array with two items in it, where each item is itself an array with 3 ints in it.

```int (*p) = a;
```

You can use p to point to either of the two items in a. p points to a three-int array--namely, the first such item. p+1 would point to the second three-int array. To initialize p to point to the second element, use:

```int (*p) = &(a);
```

The following are equivalent ways to point to the first of the two items.

```int (*p) = a; // as before
int (*p) = &(a);
```

```int a;
```

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

int (*p) = a;

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

Consider this example:

```        int a
+----+----+----+----+----+----+
|    |    |    |    |    |    |
+----+----+----+----+----+----+
\_____________/
|
|
|
p    int (*p)
```

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

Stricly speaking, no, int (*p) = a; is not a pointer to a. It is a pointer to the first element of a. The first element of a is an array of three ints. p is a pointer to an array of three ints.

A pointer to the array a would be declared thus:

```int (*q) = &a;
```

The numeric value of p and q are likely (or maybe even required to be) the same, but they are of different types. This will come into play when you perform arithmetic on p or q. p+1 points to the second element of array a, while q+1 points to the memory just beyond the end of array a.

Remember: cdecl is your friend: int a, int (*q).

The  is a part of the type. In this case p is a pointer to an array of size 3 which holds ints.

The particular type of an array always includes its size, so that you have the types int * or int *, but not just int *[] which has undefined size.

```int *x; /* type of x is int *, not just int *[] */
int y; /* type of y is int, not just int[][] */
```

you can point to 2d array like 1d array

```#include <iostream>
int main()
{
int array = {{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
}
```

Also note:

```int *p        // p is an array of 5 pointers

int (*p)      // p points to an array of 5 ints

int (*(p+5)) // p is a pointer to a structure where the structure's 5th element has 10 ints .
```