[C Language] 12-pointer to a one-dimensional array element

Source: Internet
Author: User

Note: This C language topic is a prelude to iOS development. To enable programmers with object-oriented language development experience to quickly get started with the C language. If you have no programming experience or are not interested in C and iOS development, ignore

We have learned the pointer. If the pointer stores the address of a variable, we can say that the Pointer Points to this variable. The array and its array elements both occupy the storage space and have their own addresses. Therefore, the pointer variable can point to the entire array or to the array element.

1. Use a pointer to point to the elements of a one-dimensional array
1 // define an int type array 2 int a [2]; 3 4 // define an int type pointer 5 int * p; 6 7 // Let the pointer point to the 0th elements of the array 8 p = & a [0]; 9 10 // modify the value of the pointed element 11 * p = 10; 12 13 // print the value of the first element 14 printf ("a [0] = % d", a [0]);

Output result: it indicates that the value of the array element has been indirectly modified through the pointer, which is the same as pointing to a common int type variable.

Because the array name represents the first address of the array, that is, a = & a [0], the 8th line of code is equivalent:

// Point the pointer to the 0th elements of the array p =;

The memory analysis diagram is as follows. A pointer variable occupies 2 bytes and an int array element occupies 2 bytes.

 

2. Use pointers to traverse array elements 1. The most common Traversal method is to use array subscript to traverse Elements
1 // define an array of the int type 2 int a [4] = {1, 2, 3, 4}; 3 4 int I; 5 for (I = 0; I <4; I ++) {6 printf ("a [% d] = % d \ n", I, a [I]); 7}

Output result:

 

2. Next we will use pointers to traverse array elements.

First define a pointer pointing to the first element of the array

// Define an int type array int a [4] = {1, 2, 3, 4}; // define an int type pointer, and point to the 0th elements of the array int * p =;

The value of p is the address of a [0]. Therefore, we can use the pointer p to access only The 0th elements of an array, a [0]. use * p to retrieve the value 1 of a [0. To access other elements, you must obtain the element address. We can find that the difference between the addresses of each element is 2, because in a 16-bit compiler environment, an int type variable occupies 2 bytes. Now I only know that the address value of a [0] is p. How can I obtain the address of other elements based on the address of a [0? In fact, p + 1 is the address of a [1. Note that p + 1 indicates that the value of p is 2, not the value of p plus 1. For example, the value of p is ffc3, and the value of p + 1 is ffc5, instead of ffc4. Similarly, p + 2 is the address ffc7 of a [2], and p + 3 is the address ffc9 of a [3.

Let me explain, why does p + 1 mean that the value of p is increased by 2 instead of 1?

In fact, p + 1 does not necessarily mean that p values are added with 2, or 1, 4, or 8. The value is related to the pointer type. It is in a 16-bit compiler environment.

Clever, you may have found the rule. Because char variables occupy 1 byte, p + 1 indicates that the value of p is increased by 1; float variables occupy 4 bytes, therefore, p + 1 represents the value of p plus 4. From this point, we can also explain why pointers must be classified into different types. p + 1 has different meanings for different types of pointers.

In the above Code, p points to the array element a [0] of the int type, so p + 1 represents the value of p plus 2. If you know how to get the addresses of other elements, you can use the pointer p to traverse the array elements.

1 // define an int type array 2 int a [4] = {1, 2, 3, 4}; 3 4 // define an int type pointer, and points to the 0th elements of the array 5 int * p = a; 6 7 int I; 8 for (I = 0; I <4; I ++) {9 // use the pointer operator * to retrieve the value of the array element 10 int value = * (p + I ); 11 12 printf ("a [% d] = % d \ n", I, value); 13}

Pay attention to the 10th line of code. * (p + I) indicates accessing the corresponding bucket based on the value of p + I (actually the address of the array element I, and take out the stored content (that is, the value of the array element I), and assign the value to the left.

The final output result is the same :. Note: After the traversal is complete, the pointer Variable p still points to a [0], because the p value has never changed and is always the address ffc3 of a [0.

In addition, the following code can be changed to line 1:

int value = *(a+i);

As we all know, the value a represents the first address of the array, that is, the address ffc3 of a [0. A + 1 represents the value of a plus 2, that is, the address ffc5 of a [1], that is, a + I represents the address of element a [I. I believe you can also guess that a + 1 does not necessarily mean that the value of a plus 2 depends on the array type. The Calculation Method of a + I is the same as that of p + I.

 

After traversing the array elements using the above method, p points to element a [0]. In fact, we can also directly modify the value of p to access the array elements. You only need to modify the code of Line 1.

// Use the pointer operator * to retrieve the int value of an array element = * (p ++ );

P ++ is actually equivalent to p = p + 1, directly modifying the p value, and adding 2 at a time. Therefore, every time p ++ is executed, the pointer p points to the next array element.

The output result must be the same :. However, after the traversal, the pointer Variable p does not point to any array element, because a total of four p ++ operations are performed, and the final p value is ffcb. Of course, you can re-point p to a [0]: p = & a [0]; or p =;

Note: The writing method here is incorrect.

int value = *(a++);

A ++ is equivalent to a = a + 1, and array name a is a constant! Value assignment is not allowed!

 

Iii. Summary of pointers and Arrays

P is a pointer, and a is an array.

1> If p points to an array element, p + 1 points to the next element of the array element. For example, if p = & a [0], p + 1 indicates the address of a [1 ].

2> for different types of array elements, the change of P values is different. If the array element is of the int type, 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 ],

  • Both p + I and a + I can represent the address of element a [I]. They all point to the I-th element of the array. A Represents the first address of the array, and a + I is also the address. Its calculation method is the same as that of p + I.
  • * (P + I) and * (a + I) both represent the array element a [I]
  • Although p + I and a + I both point to the I-th element of the array, there is a difference between the two. Because p as a pointer variable can change its own value, such as p ++, so that the value of p is automatically increased. Array name a is a constant that represents the first address of the array. Its value cannot be changed, that is, a ++ is invalid.

4> there are two ways to reference an array element:

  • Subscript method: for example, a [I]
  • Pointer method: for example, * (p + I) or * (a + I)

 

Iv. array, pointer, and function parameters

1. when the array name is used as the real parameter of the function, the first address of the real parameter array is passed to the form parameter array. The two arrays occupy the same memory space, in this way, the element values in the input parameter array will change at the same time.

1 void change (int B []) {2 B [0] = 10; 3} 4 5 int main () 6 {7 // define an array of the int type 8 int a [4] = {1, 2, 3, 4 }; 9 10 // pass array name a into the change function 11 change (a); 12 13 // view a [0] 14 printf ("a [0] = % d ", a [0]); 15 16 return 0; 17}

The parameters of the change function are of the array type. When the change function is called in row 11th, the array name a, that is, the address of the array, is passed to array B. Therefore, arrays a and B occupy the same memory space.

Output result:

 

2. This address can also be transferred using pointers. The real parameters and form parameters of the function can both use arrays or pointers. In this case, there are four situations:

That is to say, if the input parameter type of a function is an array, you can pass in the array name or pointer variable when calling the function;

1 void change (int B []) {2 B [0] = 10; 3} 4 5 int main () 6 {7 // define an array of the int type 8 int a [4] = {1, 2, 3, 4}; 9 10 int * p =; 11 12 // pass array name a into the change function 13 change (p); 14 15 // view a [0] 16 printf ("a [0] = % d ", a [0]); 17 18 return 0; 19}

Note that the parameter type of row 1st is an array of int B []. Row 10th defines the pointer Variable p, and row 13th uses p as the real parameter to input the function.

 

If the type of a function parameter is a pointer variable, You can input an array name or pointer variable when calling the function.

1 void change (int * B) {2 B [0] = 10; 3 // or * B = 10; 4} 5 6 int main () 7 {8 // defines an array of the int type 9 int a [4] = {1, 2, 3, 4 }; 10 11 // pass array name a into the change function 12 change (a); 13 14 // view a [0] 15 printf ("a [0] = % d ", a [0]); 16 17 return 0; 18}

Note that the parameter type of row 1st is a pointer variable int * B, and row 12th array name a is used as the real parameter input function.

From the 2nd rows, we can see that in many cases, pointers and arrays can be switched to each other. However, pointers are not equal to arrays.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.