Document directory
- 1. array pointer (pointer to array)
- 2. pointer array (array for storing pointers)
- 4. References
1. array pointer (pointer to array)
(1) Representation of arrays in memory
Creating an array is to open up a continuous space in the memory, such as int A [4]; it is to open up a memory space of 4 * sizeof (INT) bytes in the memory. A two-dimensional array is a special one-dimensional array.
Let's take a look at a piece of code:
Void main () {int A [2] [2] = {1, 2, 3, 4}; // This is a 2*2 two-dimensional array int (* P) [2]; // array pointer P = A; // point P to array}
Note this sentence in the Code: int (* P) [2]; here P is an array pointer variable.
Each element in a is stored in the memory as follows:
(2) Understand array names and array pointer Variables
Okay, now let's think about what a, a [0], a [1], P, A + 1, a [0] + 1, p + 1 is, think for 3 seconds:
Analysis:
A is an array name. The type is a pointer to a one-dimensional array. It is not a variable. The value of A is a pointer constant. That is, operations such as a ++ or a = P are not allowed. A points to the first address of this contiguous space. The value is & A [0] [0].
A [0] is a one-dimensional array name. Its type is a pointer to an integer. Its value is & A [0] [0]. This value is a constant.
A [1] is a one-dimensional array name. Its type is a pointer to an integer. Its value is & A [1] [0]. This value is a constant.
P is an array pointer variable pointing to a one-dimensional array pointer variable. The value is & A [0] [0]. You can perform operations such as P ++ and P =.
A + 1 points to the next element, which can also be understood as pointing to the next one-dimensional array.
* (A + 1) is the first address of the first line.
A [0] + 1 is the first element pointing to the first element of the one-dimensional array a [0.
P + 1 is the same as a + 1
* (P + 1) Same as * (a + 1)
Although the value of A is the same as that of a [0], its type is different and its meaning is different. Through analysis, it is not difficult to understand why * (a + I) + J) is equivalent to a [I] [J.
(3) the pointer is an array iterator.
#include<stdio.h>#define M 2#define N 3int main(){int a[M][N]={1,2,3,4,5,6};int *start=&a[0][0];int * const end=start+M*N;for(;start!=end;start++)printf("%-5d",*start);putchar('\n');return 0;}
Understand this code and use pointers to traverse a two-dimensional array. Isn't it like the vector iterator in the C ++ standard library. Note that only one for loop is used here. This also shows that two-dimensional arrays are actually special one-dimensional arrays.
(4) differences between array names and array pointer Variables
From the analysis in (2), the array name is a pointer, And the type is a pointer to the element type, but the value is a pointer constant, when an array is declared, the compiler reserves the memory space for the number of elements specified for the declaration. An array pointer is a pointer to an array. When declaring a pointer variable, the compiler only reserves the memory space for the pointer itself.
Look at this Code:
# Include <stdio. h> void main () {int A [2] [2] = {1, 2, 3, 4}; // This is a 2*2 two-dimensional array int (* P) [2]; // array pointer P = A; // point P to array aprintf ("% d \ n", sizeof A, sizeof P );}
Guess what the output is?
Are you confused? Why is the result like this? Let's take a preliminary look at the sizeof keyword. below is the description of sizeof on msdn:
Note the red font in the description. When sizeof is used for a variable, the actual space occupied by the variable is returned. When sizeof is used for Array names, the size of the entire array is returned (the size here refers to the number of bytes occupied ). P is a pointer variable, which occupies four bytes. A is the array name, so sizeof a returns the number of bytes occupied by all elements in array.
After learning about sizeof, guess what the following code outputs.
# Include <stdio. h> void main () {int A [2] [2] = {1, 2, 3, 4}; // This is a 2*2 two-dimensional array int (* P) [2]; // array pointer P = A; // point P to array a printf ("% d \ n", sizeof (a + 1 ), sizeof (p + 1); printf ("% d \ n", sizeof (a + 0), sizeof (p + 0 ));}
Running result:
The result shows that A is converted into a pointer variable when performing the + operation. In this case, the + I type is a pointer variable rather than an array name. However, a [I] is the array name of a one-dimensional array, and the value of sizeof (A [0]) is 8..
Now let's look at a piece of code:
# Include <stdio. h> void F (int A [] [2]) {printf ("% d \ n", sizeof A);} void main () {int A [2] [2] = {1, 2, 3, 4}; // This is a 2*2 2D array printf ("% d \ n", sizeof ); f ();}
Guess what the output is?
Is it a bit confusing?
Explanation: this is because the array name is converted into a pointer variable when passing the parameter. Note that the length of the two-dimensional array is not required here in F (int A [] [2]) of the function, here you can change it to int (* A) [2]. Therefore, it is an array pointer variable. That's clear!
Summary: The array name type is a pointer to the element type, and the value is a pointer constant. (A + 1) is a pointer variable. When the array name is passed as a parameter, it is actually a pointer variable. Sizeof returns different results for operations on variables and array names. An array pointer is a pointer to an array. Its value can be a variable.
2. pointer array (array for storing pointers)
(1) Recognize pointer Arrays
An array containing the int type is called an integer array, so the array storing the pointer is called a pointer array.
# Include <stdio. h> void main () {int I = 1, j = 2; // P is first combined with [], and then combined with * int * P [2]; // pointer array, which stores the pointer array P [0] = & I; P [1] = & J; printf ("% d", sizeof (p ));}
Breakpoint debugging analysis:
In this example, array P has two elements. P [0] is the pointer to I, and P [1] is the pointer to J. Both pointers are int type pointers, so P is an array that stores int type pointers. Sizeof (p) returns the total space occupied by the array, so the program output is 8
(2) Example of pointer array usage
An example from the C programming language sorts the strings. After reading the following example, I believe you have realized the advantages of the pointer array.
3. In-depth consideration
Array names a, a [0], and a [1]. How is the array pointer P stored in the memory? Where is it stored in the memory? Is it in the code segment? Please try again!
4. References
C and pointer, the C programming language, etc.