In this paper, some techniques for understanding C + + pointer arrays, array pointers, array names, and two-dimensional arrays are analyzed in detail. is a more important concept, I believe that for everyone's C + + program design has a certain role in helping.
First, about the array name
Suppose you have an array:
1. The array name represents the address of the first element of the array , noting that not the array address (although the value is equal) is the first element address of the array, and a is equivalent to &a[0];
A+1 is the address of the second element. than the first element address a (or &a[0]) exceeds the size of an integer pointer, which is 4 bytes (byte)
cout << a << endl;//will output the first element address of the array.
2. Access symbols and.
&a is the address of the array, note the address of the array, which represents the address of this whole array. is not the address of the first element of the array (although their values are the same)
&a+1 than the address of an array &a the address size of an array, here is the 3*4 byte
int * p = &a; This statement is not valid. The left pointer variable p points to an integer pointer, and the right is the address of the array (type is an array), not the address of the array element (type is an integer), so it cannot be assigned a value.
you should assign values to the array pointer (described below).
Remember the above two points about the array name.
Two, about the pointer array
1. Define
An array of pointers, an array of pointers, in which the elements in an array are pointers (in contrast to an integer array, an integer array is an array of integral types, and the elements in the array are integers).
int *ptr[3]; How to understand? by operator precedence, [] priority is greater, so the PTR is combined with [3] to indicate that the PTR is an array, and that the element type of the array is necessarily explicit, so the element type in the array is an integer pointer (int*), and the size of the array is not necessarily required (the array can be defined according to the number of elements initialized)
Ptr[0] is the 0th element of an array, which is an integer pointer.
Examples are as follows:
int a[3] = {1, 2, 3};
int x = 5;
Ptr[0] = &x;
PTR[1] = &a[2];
2. How to use?
Used like a normal pointer. *PTR[0] is the value of the element that the 0th element (a pointer) points to, here is 5.
Three, about array pointers
1. Define
The array pointer, which is a pointer to an array, is a pointer to an array (a pointer to an integer pointer to an integral type, a pointer to an integer).
int (*PTR) [3]; How to understand? Look at the parentheses, *ptr that PTR is a pointer, and then combine with [] to show that the pointer points to an array whose element is an int
int a[3] = {1, 2, 3};
int (*PTR) [3] = a;//This statement does not hold true.
Right A is an array name, remember what it says, the array name represents the address of the first element of the array, which is &a[0], and the type of the array name is the same as the integer pointer (not knowing it is actually not) int *, because it points to the first element, the first element is int
The type of PTR on the left is int (*) [], an array pointer, a pointer to an array, not a pointer to an integral type, and cannot be assigned a value.
int a[3] = {1, 2, 3};
int (*PTR) [3] = &a;//correct.
Because a is an array, &a is the address of the array, do you remember what it said?
2. How to use?
int a[3] = {1, 2, 3};
int (*PTR) [3] = &a;
cout << (*PTR) [0] << Endl; Output 1
cout << (*PTR) [1] << Endl; Output 2
It's a little hard to understand here. Don't try to contrast the code.
int a[3] = {1, 2, 3};
int x = 5;
int * p = &x;
cout << *p << Endl; Output 5
P is a pointer to an integral type, and *p is the value of the variable (integer x) that you are pointing to. Empathy ptr is a pointer to an array, and *ptr is the value of the variable (array a) that you are pointing to. (*PTR) [0] is the 0th element of the array.
Iv. about two-dimensional arrays
1. A two-D array is an array whose elements are one-dimensional arrays . Keep that in mind, and then put the upper sleeve on the line.
int a[3][3] = {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Array name
A is the address of the first (or 0th better) element of the array, the first element 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, exceeding the 3*4 byte
&a is the address of an array, &a+1 is beyond the size of a two-dimensional array, exceeding 3 * 4 * 3 bytes.
Array pointers
int (*PTR) [3] = A; That's right.
Because a represents the address of the first element, the first element is a one-dimensional array, so a represents the address of a one-dimensional array, the address of an array is assigned to the array pointer, and is set up.
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.
3. An array of pointers is an array, and its elements are pointers.
4. The array pointer is a pointer that points to an array.
5. the elements of a two-dimensional array are one-dimensional arrays.