Dark Horse programmer--c Pointers in language (2)

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

Description and use of array pointer variables

Pointer variables that point to an array are called array pointer variables. Before we discuss the description and use of array pointer variables, we will first clarify a few relationships. An array is made up of contiguous blocks of memory. The array name is the first address of this contiguous memory unit. An array is also made up of individual array elements ( subscript variables ) . Each array element occupies several contiguous memory units, 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 to an array element, assigning the array name or the address of the first element to it. If you want the pointer variable to point to the number I element, you can Assign the first address of the I element to it or assign the array name plus i to it.

With a real groupa, point toathe pointer variable isPA, from the figure6.3we can see that there are the following relationships:PA,A,&A[0]all point to the same cell, which is an arrayaThe first address, also0number ElementA[0]'s first address. Pa+1,a+1,&a[1]all point to1number ElementA[1]. AnalogyA+i,a+i,&a[i]PointingInumber ElementA[i]. It should be stated thatPAis a variable, andA,&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"); }

The main function defines an integer array and an integer variable looping statement to assign values to the array to print the values of each group ... Output line Wrapping ... The general form of the array pointer variable description is: Type descriptor * pointer variable name

Where the type specifier represents the type of the index group. As you can see from the general form, pointer variables that point to an array are the same as those for pointer variables that point to normal variables. With the introduction of pointer variables, you can access array elements in two ways.

The first method is the subscript method, which accesses the array element in the form of A[i] . This approach is used in the introduction of arrays in the fourth chapter.   

The second method is the pointer method, which uses the form of * (pa+i) to access the array elements using the indirect access method.

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++;

}} The main function defines an integer array and pointerPApoint to ArrayaLoops the variableIvalue is assigned to the pointer by thePApoint to thea[]the array unitThe pointerPAPointinga[]the next unit...... PointerPAgetting the array backa's first addressLoop Array Output Arrayaall of the elements inThe pointerPAPointinga[]the next unit

Here, another example, the 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++);

}} The main function defines an integer array and pointer, and causes the pointer to point to the array a loop that assigns the value of the variable i to the a[] that is pointed to by the pointer pa . use the pointer to output all elements of array a , while the pointer PA points to the next cell of a[]

Array names and arrays of pointer variables as function arguments

In the fifth chapter, we have introduced the problem of using the arguments and parameters of the array famous function. It is easier to understand the problem after learning the pointer variable. The array name is the first address of the array, and the actual parameter to the parameter transfer array name is actually the address of the transfer array, and the parameter gets the address and points to the same array. It's like having two different names for the same item. Similarly, the value of the pointer variable is also an address, and the value of the array pointer variable is the first address of the group, and of course it can be used as a function parameter.

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; }

Dark Horse programmer--c Pointers in language (2)

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.