one, pointing to the elements of a one-dimensional array
1//Defines an array of type int2int a[2];34//Defines a pointer of type int5int *P 7 // Let the pointer point to the NO. 0 element of the array 8 p = &a[0 9 10 // Modify the value of the pointed element 11 *p = 10;< Span style= "color: #008080;" >12 13 // print the value of the first element Span style= "color: #008080;" >14 printf ( "a[0] =%d" A[0]);
Output: Indicates that the value of an array element has been indirectly modified by a pointer , just as it would be for a normal int type variable.
Because several groups of names represent the first address of an array, a = = &a[0], the 8th line of code is equivalent to:
let the pointer point to the NO. 0 element of the array p = A;
The memory analysis diagram is as follows, a pointer variable occupies 2 bytes, an array element of type int occupies 2 bytes
second, use the pointer to iterate over the array elements1. The most common way to traverse is to use an array subscript to traverse the element
1//Defines an array of type int2 int a[4 "= {1, 2, 3, 4}; 3 4 int I; 5 for (i = 0; I < 4; I++) {6 printf ( "A[%D] =%d \n "
Output Result:
2. Next we use pointers to iterate over array elements
First define a pointer to the first element of the array
defines an array of type int a[4] = {4}; defines a pointer to an int type and points to the No. 0 element of the array int *p = A;
The value of P is a[0], so now we can only access the No. 0 element of the array using the pin P a[0], the value 1 of a[0] can be removed with *p. To access other elements, you have to get the address of the element, and you can see that the address difference for each element is 2, because in a 16-bit compiler environment, a variable of type int occupies 2 bytes. Now just know a[0] address value is p, how to get the address of other elements according to the address of a[0]? Actually very simple, p+1 is the address of a[1]. Note that the p+1 here represents the value of P plus 2, not the value of P plus 1, such as P's value of ffc3,p+1 is FFC5, not ffc4. And so on, P+2 is the address of a[2] ffc7,p+3 is a[3] address ffc9.
first of all, why p+1 is the value of P plus 2 instead of adding 1?
In fact, the p+1 does not necessarily represent the value of P plus 2, it may be added 1, plus 4, or plus 8. How much is added, which is related to the type of pointer. Is the case in a 16-bit compiler environment.
Smart you may have found the law, because the char type of variable to occupy 1 bytes, so p+1 represents the value of P plus 1;float type of variable occupies 4 bytes, so p+1 represents the value of P plus 4. From this point, it can also be very good to explain why pointers must be divided into types, different types of pointers, the meaning of p+1 is not the same.
P in the preceding code points to an array element of type int a[0], so p+1 represents the value of P plus 2. Knowing how to get the address of other elements, you can use the pointer p to iterate over the elements of the array.
1//Defines an array of type int2int a[4] = {1,2,3,4};34//Defines a pointer of type int and points to the No. 0 element of the array5int *p = A; 7 int I; 8 for (i = 0; I < 4; I++) { 9 // take the pointer operator * To remove the value of the array element 10 int value = * (P+i); 11 12 printf ( "A[%D] =%d \n "
Note that the code for line 10th, * (P+i), represents the value of the p+i (which is actually the address of the number of elements of the group I) to access the corresponding storage space, and takes out the stored contents (that is, the value of the number of elements of the I array), assigned to the left value.
The final output effect is the same:. Note: After the traversal, the pointer variable p still points to a[0], because the P-value has not changed, and has always been the address of a[0] ffc3.
Add, in fact, the 10th line changed to the following code is also possible:
int value = * (A+i);
As you all know, the A value represents the address of the array's first address, which is a[0] ffc3. A+1 represents the address of the value of a plus 2, or a[1], which means that a+i represents the address of the element A[i]. I believe you can also guess, a+1 does notnecessarily mean a value plus 2, exactly how much, depending on the type of array. the a+i is calculated in the same way as the p+i.
After iterating through the array elements using the above method, p always points to element a[0]. In fact, we can also directly modify the value of p to access the array elements, only need to change the 10th line of code to
Use the pointer operator * To remove the value of an array element int value = * (p++);
p++ is actually the equivalent of p = p + 1, directly modified P-value, and each is added 2. So, each time p++ is executed, the pointer p points to the next array element.
The output will certainly be the same:. However, after the traversal, the pointer variable p does not point to any of the array elements because it executes 4 times p++ and the last P-value is FFCB. Of course, you can re-point p to A[0]:p = &a[0]; or P = A;
Note that the wording here is wrong.
int value = * (a++);
a++ equivalent to a=a+1, array name A is a constant! Assignment operation cannot be performed!
Iii. Summary of pointers and arrays
P is a pointer, a is an array
1> if p points to an array element, then p+1 represents the next element that points to the array element. For example, suppose P = &a[0], then p+1 represents the address of a[1]
2> for different types of array elements, the P-value changes are different. If the array element is of type int, p+1 represents the value of P plus 2 (in a 16-bit compiler Environment)
3> if the initial value of P is &a[0], then
- Both P+i and a+i can represent the address of the element A[i], and they all point to the first element of the array. A for the first address of the array, A+i is also the address, it is calculated in the same way as P+i
- * (P+i) and * (A+i) both represent array elements a[i]
- Although both P+i and A+i point to the first element of the array, they are still different when used. Since p as a pointer variable can change its own value, such as p++, the value of P increases. The array name A is a constant representing the first address of the array, and its value cannot be changed, that is, a++ is illegal.
4> reference An array element can have two methods:
- Subscript method: such as A[i]
- Pointer method: such as * (P+i) or * (A+i)
iv. arrays, pointers, and function parameters
1. when using an array name as a function argument, the first address of the real parameter group is passed to the shape parameter group, and two arrays occupy the same memory space together, so that the element value of the parameter group changes and the element value of the real parameter group changes simultaneously.
1void Change (IntB[]) {2 b[0] =10;3}45IntMain ()6{7//Defines an array of type int8int a[4] = {1,2,3,4}; 9 10 // The array name A is passed into the change function 11 12 13 // view A[0]14 printf ( "a[0]=%d" A[0 16 return 0; 17}
The parameter of the change function is of the array type, and the array name A, which is the address of the array, is passed to array B when call the change function on line 11th. So arrays A and b occupy the same piece of memory space.
Output Result:
2. The delivery of this address can also be achieved with pointers. both the arguments and the parameters of the function can use arrays or pointers, respectively. There are 4 things that can happen:
That is, if the parameter type of a function is an array, you can pass in the array name or pointer variable when the function is called;
1void Change (IntB[]) {2 b[0] =10;3}45IntMain ()6{7//Defines an array of type int8int a[4] = {1,2,3,4};910int *p = A;12 //13 15 // view A[0]16 printf ( "a[0]=%d" A[0 18 return 0; 19}
Note that the formal parameter type of line 1th is the array int b[], the 10th line defines the pointer variable p, and 13th row p as the argument passed into the function
If the parameter type of a function is a pointer variable, you can pass in the array name or pointer variable when the function is called.
1void Change (int *b) {2 b[0] =10;3//or *b = 10;4}56IntMain ()7{8//Defines an array of type int9int a[4] = {1,2,3,4}; 10 11 // The array name A is passed into the change function 12 13 14 // view A[0]15 printf ( "a[0]=%d" A[0 17 return 0; 18}
Note that the formal parameter type of line 1th is a pointer variable int *b, and the 12th row of array name A is passed as an argument to the function.
As can be seen from line 2nd, in many cases pointers and arrays can be used to switch between each other. However, it is not possible to say that pointers are equal to arrays.
Learning note 12-pointers to elements of a one-dimensional array