C Language Learning course seventh chapter-Structure and Union (4)

Source: Internet
Author: User

These three forms for representing struct members are completely equivalent. Structure array pointer variable structure pointer variable can point to an array of structures, at which point the value of the struct pointer variable is the first address of the entire structure array. A struct pointer variable can also point to an element of a structure array, at which point the value of the struct pointer variable is the first address of the array element of the structure. Set PS to a pointer variable that points to an array of structures, PS also points to the No. 0 element of the structure array, ps+1 points to the 1th element, and ps+i to the I element. This is consistent with the case of an ordinary array.
[Example 7.7] outputs an array of structures with pointer variables.
struct STU
{
int num;
Char *name;
char sex;
Float score;
}boy[5]={
{, "Zhou ping", ' M ', 45},
{102, "Zhang ping", ' M ', 62.5},
{Liou, "Fang", ' F ', 92.5},
{Cheng, "ling", ' F ', 87},
{A, "Wang Ming", ' M ', 58},
};
Main ()
{
struct Stu *ps;
printf ("no\tname\t\t\tsex\tscore\t\n");
for (ps=boy;ps<boy+5;ps++)
printf ("%d\t%s\t\t%c\t%f\t\n",ps->num,ps->name,ps->sex,ps->
Score);
}
In the program, the outer array boy of the Stu struct type is defined and initialized. Define PS as a pointer to the STU type within the main function. In the expression 1 of the loop statement for, PS is given the first address of the boy, and then loops 5 times, outputting the values of each member in the boy array. It should be noted that although a struct pointer variable can be used to access a struct variable or a member of a struct array element, it cannot be pointed to a member. In other words, it is not allowed to take a member's address to give it. Therefore, the following assignment is incorrect. ps=&boy[1].sex; but only: Ps=boy; (give array the first address)
Or is:
PS=&BOY[0];(assigned to the first address of element No. 0)

Structure pointer variable as function parameter

The

allows structural variables to be used as a whole for function parameters in the ANSI C standard. However, the transfer of all members, especially when the member is an array, will make the transfer time and space expensive, seriously reducing the efficiency of the program. Therefore, the best way is to use the pointer, that is, using pointer variables as function parameters for transmission. At this point, the arguments are passed to the formal parameters of the address only, thereby reducing the time and space overhead. The
[Example 7.8] topic is the same as for example 7.4, which calculates the average score and the number of failed students in a group. The
is programmed with a structure pointer variable as a function parameter.
struct stu
{
int num;
Char *name;
Char sex;
Float score;} Boy[5]={
{f, "Li ping", ' m ', '},
{102, "Zhang ping", ' m ', 62.5},
{, "he fang", ' F ', 92.5},
{), "Cheng Ling ", ' F '," I ",
{A," Wang Ming ", ' M ',"},
};
Main ()
{
struct stu *ps;
void Ave (struct Stu *ps);
Ps=boy;
Ave (PS);
}
Void Ave (struct Stu *ps)
{
int c=0,i;
float ave,s=0;
for (i=0;i<5;i++,ps++)
{
S+=ps->score;
if (ps->score<60) c+=1;
}
printf ("s=%f\n", s);
AVE=S/5;
printf ("average=%f\ncount=%d\n", ave,c);
}
The function Ave is defined in this program, and its parameters are PS of the structure pointer variable. Boy is defined as an array of external structures and is therefore valid throughout the source program. The structure pointer variable PS is defined in the main function, and the first address of the boy is given to it, so that PS points to the boy array. Then invoke the function Ave with PS as the argument. The work of calculating average scores and failing numbers in the function Ave is completed and the results are output. Compared with example 7.4 program, because this program uses pointer variable for operation and processing, so the speed is faster and the program efficiency is higher.

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.