"c language" Pointer to a one-dimensional array element

Source: Internet
Author: User

directory of this document
    • one, pointing to the elements of a one-dimensional array
    • second, Use the pointer to iterate over the array elements
    • Iii. summary of pointers and arrays
    • Iv. arrays, pointers, and function parameters


We have learned the pointer before, and if the pointer stores the address of a variable, we can say that the pointer points to the VARIABLE. Arrays and their array elements both occupy storage space and have their own addresses, so pointer variables can point to an entire array or to an array element.

Back to Topone, pointing to the elements of a one-dimensional array
1//Defines an array of type int2int a[2];34//Defines a pointer of type intint *p; 6  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; 12 13 // Print the value of the first element 14 printf ( a[0] =%d0]);        

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

Back to Topsecond, 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
defines an array of type int a[4] = {4};  int i; 4; I++) {6 printf ("a[%d] =%d \ n", i, a[i]);  7}          

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.

let me explain why p+1 represents 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 array  5 int *p = A; 6  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 \n13}             

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!

Back to TopIii. 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)

Back to Topiv. 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 (int B[]) {2 b[0] =10;3}45int main ()6 {7//Defines an array of type int8int a[4] = {1, 2, 3, 4};  9 10 //11 change (a); 13 // view A[0]14 printf ( "a[0]=%d" A[0]); 15 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 (int B[]) {2 b[0] =10;3}45int main ()6 {7//Defines an array of type int8int a[4] = {1,2,3,4};  9 10 int *p = A;11 12 //13 The change (p); 15 // view A[0]16 printf ( "a[0]=%d" A[0]); 17 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}56int main ()7 {8//Defines an array of type int9int a[4] = {1, 2, 3, 4}; 10 11 //12 change (a); 14 // view A[0]15 printf ( "a[0]=%d" A[0]); 16 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.

"c language" Pointer to a one-dimensional array element

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.