C language 12-pointer to a one-dimensional array element

Source: Internet
Author: User

    • 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

Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are not interested in C or iOS development, please ignore.

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.

One, pointing to the elements of a one-dimensional array
1//define an array of type int 2 int a[2]; 3  4//define an int type pointer 5 int *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 *p = 10;12 13//Print the value of the first element printf ("a[0] =%d", a[0]);

Output Result:

Indicates that the value of an array element has been indirectly modified by pointers, just as it is for a normal int type variable.

Because the array name represents the first address of the group, 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 elements

1. The most common way to traverse is to use an array subscript to traverse the element

1//define an array of type int 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 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] = {1, 2, 3, 4};//defines a pointer of type int and points to the No. 0 element of the array int *p = A;

The value of P is the address of a[0], so now we can only access the No. 0 element of the array using pointer P a[0], and *p can remove the value of a[0] 1. 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//define an array of type int 2 int a[4] = {1, 2, 3, 4}; 3  4//Define a pointer of type int and point to the No. 0 element of the array 5 int *p = A; 6  7 int i; 8 for (i = 0; i < 4; i++) {9     //Use pointer operator * To remove the value of the array element 1 0     int value = * (p+i);     printf ("a[%d] =%d \ n", I, value); 13}

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 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 not necessarily 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.

1 void change (int b[]) {2     b[0] = 3} 4  5 int main () 6 {7     ///define an array of type int 8     int a[4] = {1, 2, 3, 4}; 9     //The array name A is passed in the change function one by one     (a)     ;     //View a[0]14     printf ("a[0]=%d", A[0]);     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;

1 void change (int b[]) {2     b[0] = 3} 4  5 int main () 6 {7     ///define an array of type int 8     int a[4] = {1, 2, 3, 4}; 9     int *p = a;11     //The array name A is passed into the change function in the     [P];     //view a[0]16     printf ("A [0]=%d ", a[0]); +     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.

1 void change (int *b) {2     b[0] = 3/     /or *b = 4} 5 6  int main () 7 {8     //Define an array of type int 9     int a[ 4] = {1, 2, 3, 4};10     //The array name A is passed into the change function in     a (a);     //view a[0]15     printf ("a[0]=%d", A[0]) ; +     +     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 12-pointer to a one-dimensional array element

Related Article

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.