C++ pointer arrays array Pointers array names and 2d array tips

  • 2020-04-02 02:40:29
  • OfStack

This article has analyzed in more detail about understanding C++ pointer array, array pointer, array name, two-dimensional array some techniques. Is a more important concept, I believe that the C++ programming has a certain help.

One, about array names

Suppose there is an array:


int a[3] = {1, 2, 3}

1. The array name represents the address of the first element of the array Notice, not the address of the array (although the values are the same), but the address of the first element of the array & A, [0].

A plus 1 is the address of the second element. Than the first element address a (or & A [0]) exceeds the size of an integer pointer, in this case four bytes.


cout << a << endl;//It prints the address of the first element of the array.

2. Address symbol & .

& A is the address of the array, remember the address of the array, the address of the whole array. Not the address of the first element of the array (although they have the same value)

& A +1 over the address of the array & A exceeds the address size of an array, in this case 3 by 4 bytes

Int * p = & A; This statement is not true. The left pointer to p is a pointer to an integer, and the right is the address of an array (type is an array), not the address of an array element (type is an integer), so it cannot be assigned.
Array Pointers should be assigned values (more on that later).

Remember the above two points about array names.

Two, about pointer array

Definition 1.

Pointer array is an array that holds Pointers, and the elements in the array are Pointers.

Int * PTR [3]. How to understand? According to the operator priority, [] has a higher priority, so PTR is first combined with [3] to indicate that PTR is an array, so it is necessary to specify the element type of the array, so the element type in the array is an integer pointer (int*), the size of the array is not necessary (the number of elements can be determined according to the number of initialization when defining the array).

PTR [0] is the 0th element of the array, which is an integer pointer.

Here's an example:


int a[3] = {1, 2, 3};
int x = 5;
ptr[0] = &x;
ptr[1] = &a[2];

2. How to use it?

Used like a pointer. * PTR [0] is the value of the element to which the 0th element (a pointer) refers, in this case 5.

Three, about the array pointer

Definition 1.

An array pointer is a pointer to an array. It's a pointer to an array.

Int (* PTR) [3]; How to understand? Inside the parentheses, * PTR indicates that PTR is a pointer, and then when combined with [] indicates that the pointer points to an array whose elements are int


int a[3] = {1, 2, 3};
int(*ptr)[3] = a;//This statement does not hold.

The right hand side a is the name of the array, remember, the name of the array is the address of the first element of the array, which is & A [0], the type of the array name is equivalent to an int *, because it points to the first element, which is an int

The left PTR is of type int(*)[], which is a pointer to an array, not an integer, and cannot be assigned.


int a[3] = {1, 2, 3};
int (*ptr)[3] = &a;//Correct.

Because a is an array, & A is the address of the array, remember that?

2. How to use it?


int a[3] = {1, 2, 3};
int (*ptr)[3] = &a;

cout << (*ptr)[0] << endl;  //Output 1
cout << (*ptr)[1] << endl;  //The output of 2

There's a little bit of a puzzle here. Compare the code.


int a[3] = {1, 2, 3};
int x = 5;
int * p = &x;
cout << *p << endl;  //The output of 5

P is a pointer to an integer, and *p is the value of the variable (integer x). Similarly, PTR is a pointer to an array, and * PTR is the value of the variable (array a). (* PTR)[0] is the 0th element of the array.

About two-dimensional arrays

A two-dimensional array is an array whose elements are one-dimensional arrays . Keep that in mind and just slip the top in.


int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

An array of

A is the address of the first (or better yet, the 0th) element of the array, which is a one-dimensional array, a[0] -- > {1, 2, 3}. A +1 is the address of the second element, which is the address of the second one-dimensional array, beyond 3 by 4 bytes

& A is the address of the array, & A plus 1 is more than the size of a two-dimensional array, more than 3 times 4 times 3 bytes.

Pointer to an array


int (*ptr)[3] = a; //Correct.

Because a represents the address of the first element, and the first element is a one-dimensional array, so a represents the address of a one-dimensional array, and the address of an array is assigned to an array pointer, true.

V. summary:

1. The array name represents the address of the first element of the array.

2. & A (a is an array) is the address of the array.

A pointer array is an array whose elements are Pointers.

An array pointer is a pointer to an array.

The elements of a two-dimensional array are one-dimensional arrays.


Related articles: