C Language Learning tutorial sixth chapter-pointers (3)

Source: Internet
Author: User
Tags arrays integer printf

Description and use of array pointer variables

The pointer variable that points to an array is called an array pointer variable. Before we discuss the description and use of array pointer variables, let's clarify a few relationships.
An array is made up of contiguous blocks of memory. The array name is the first address of the contiguous memory unit. An array is also made up of individual array elements (subscript variables). Each array element occupies several contiguous units of memory, depending on its type. The first address of an array element is also the first address of several memory units it occupies. A pointer variable can either point to an array or point to an array element, giving it an array name or the address of the first element. If you want the pointer variable to point to element I, you can assign the first address of the I element to it or give it the array name plus I.

With the real number group A, the pointer variable to a is PA, and from Figure 6.3 we can see the following relationships:
Pa,a,&a[0] All point to the same unit, which is the first address of array A and the first address of element No. 0 A[0]. PA+1,A+1,&A[1] All point to number 1th element a[1]. Analogy A+i,a+i,&a[i]
Point to Element I, a[i]. It should be explained that the PA is a variable, and a,&a[i] are constants. Should be noted when programming.
Main () {
int a[5],i;
for (i=0;i<5;i++) {
A[i]=i;
printf ("a[%d]=%d\n", I,a[i]);
}
printf ("\ n");
}
Main function
Defines an integer array and an integer variable
Loop statement
assigning values to arrays
Print the value of each array
......
Output line Wrap
......
The general form of the description of the array pointer variable is:
Type descriptor * Pointer variable name
Where the type descriptor represents the type of the index group. As you can see from the general form, the pointer variable pointing to the array and the pointer variable pointing to the generic variable are the same description.
With the introduction of pointer variables, there are two ways to access array elements.
The first method is the subscript method, which is to access the array elements in A[i. This approach is used in the introduction of arrays in chapter fourth.
The second method is the pointer method, which uses the form of * (Pa+i) to access the array elements by means of indirect access.
Main () {
int A[5],i,*pa;
Pa=a;
for (i=0;i<5;i++) {
*pa=i;
pa++;
}
Pa=a;
for (i=0;i<5;i++) {
printf ("a[%d]=%d\n", I,*PA);
pa++;
}
}
Main function
Defining integer arrays and pointers
Point pointer pa to array a
Cycle
Assign the value of the variable I to the array cell of the a[that is pointed to by the pointer PA
Next cell pointing the pointer pa to a[]
......
Pointer PA Gets the first address of array a again
Cycle
Output all elements in array a by array
Next cell pointing the pointer pa to a[]
......
......
Here, another example, this example is the same as the previous example, but the implementation is different.
Main () {
int a[5],i,*pa=a;
for (i=0;i<5;) {
*pa=i;
printf ("a[%d]=%d\n", i++,*pa++);
}
}
Main function
Defines an integer array and pointer, and points the pointer to an array of
Cycle
Assign the value of the variable I to the array cell of the a[that is pointed to by the pointer PA
Outputs all the elements in array A with the pointer, while the pointer pa points to the next cell in a[]
......
......

Array names and arrays of pointer variables as function parameters

In the fifth chapter, we have introduced the problem of the real parameters and formal parameters of the function of array masterpieces. It's easier to understand the problem after learning the pointer variable. The array name is the first address of the array, and the argument sends the array name to the parameter that is actually the address of the routing array, and the formal parameter points to the same array after the address. It's as if there are two different names for the same item. Similarly, the value of the pointer variable is also the address, and the value of the array pointer variable, which is the first address of a group, can be used as a parameter of the function as well.
Float aver (float *pa);
Main () {
float SCO[5],AV,*SP;
int i;
Sp=sco;
printf ("\ninput 5 scores:\n");
for (i=0;i<5;i++) scanf ("%f", &sco[i));
Av=aver (SP);
printf ("Average score is%5.2f", AV);
}
Float aver (float *pa)
{
int i;
float av,s=0;
for (i=0;i<5;i++) s=s+*pa++;
AV=S/5;
return AV;
}

Pointer variable pointing to multidimensional array

This section describes the pointer variables of a multidimensional array as an example of a two-dimensional array.

One, multidimensional array address representation method
is provided with an integer two-dimensional array a[3][4]:
0 1 2 3
4 5 6 7
8 9 All
Set the first address of array A to 1000, the first address of each subscript variable and its value as shown in the figure. In the fourth chapter, the C language allows a two-dimensional array to be decomposed into multiple one-dimensional arrays for processing. So array a can be decomposed into three one-dimensional arrays, that is, a[0],a[1],a[2]. Each one-dimensional array contains four elements. For example, the a[0] array, containing a[0][0],a[0][1],a[0][2],a[0][3] four elements. The address of an array and an array element is represented as follows: A is a two-dimensional array name and is the first address of a two-dimensional array of 0 rows, equal to 1000. A[0] is the array name and first address of the first one-dimensional array, and therefore also 1000. * (a+0) or *a is equivalent to A[0], which represents the first address of a one-dimensional array a[0]0 number element. Also for 1000. &a[0][0] is the first address of the 0 row 0 column element of a two-dimensional array A, also 1000. Therefore, a,a[0],* (a+0), *a,&a[0][0] are equal. Similarly, a+1 is the first address of a two-dimensional array of 1 lines, equal to 1008. A[1] is the array name and the first address of the second one-dimensional array, and therefore also 1008. &a[1][0] is the 1-row 0-column element address of a two-dimensional array A and 1008. So a+1,a[1],* (a+1), &a[1][0] is equivalent. It can be concluded that: a+i,a[i],* (a+i), &a[i][0] is equivalent. In addition, &a[i] and a[i] are also equivalent. Because &a[i] cannot be understood as the address of an element a[i in a two-dimensional array, there is no element a[i].

The C language stipulates that it is an address calculation method that represents the array a, the first header address. From this, we conclude: a[i],&a[i],* (a+i) and a+i are equal. In addition, A[0] Also
Can be seen as the first address of the number No. 0 element of a one-dimensional array a[0], and A[0]+1 is the first address of the number 1th element of a[0], from which A[I]+J is the first address of the J element of a one-dimensional array a[i], which equals &a[i][j a[0]+0. by a[i]=* (a+i) a[i]+j=* (a+i) +j, because * (a+i) +j is the first address of the J-column element of the I line of two-dimensional array a. The value of this element is equal to * (* (a+i) +j).
[Explain] #define PF "%d,%d,%d,%d,%d,\n"
Main () {
static int a[3][4]={0,1,2,3,4,5,6,7,8,9,10,11};
printf (pf,a,*a,a[0],&a[0],&a[0][0]);
printf (pf,a+1,* (a+1), a[1],&a[1],&a[1][0]);
printf (pf,a+2,* (a+2), a[2],&a[2],&a[2][0]);
printf ("%d,%d\n", a[1]+1,* (a+1) +1);
printf ("%d,%d\n", * (a[1]+1), * (* (a+1) +1));
}

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.