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

Source: Internet
Author: User
Tags ming printf

Array of structures

The elements of an array can also be of a struct type. Therefore, you can form an array of structures. Each element of a structure array is a subscript structure variable with the same struct type. In practical applications, an array of structures is often used to represent a group with the same data structure. such as a class of student files, a workshop worker's salary list.
A structure array is defined in a way that is similar to a struct variable, simply by stating that it is an array type. For example:
struct STU
{
int num;
Char *name;
char sex;
Float score;
}BOY[5];
Defines an array of structures boy1, a total of 5 elements, boy[0]~boy[4]. Each array element has a structural form of struct Stu. An array of external structures or static structures can be initialized with an assignment, for example:
struct STU
{
int num;
Char *name;
char sex;
Float score;
}boy[5]={
{, "Li ping", "M", 45},
{102, "Zhang ping", "M", 62.5},
{Fang, "he", "F", 92.5},
{Cheng, "Ling", "F", 87},
{A, "Wang Ming", "M", 58};
}
When all elements are initialized, the length of the array can also be given.
[Example 7.4] calculates the student's average score and the number of people who fail.
struct STU
{
int num;
Char *name;
char sex;
Float score;
}boy[5]={
{, "Li ping", ' M ', 45},
{102, "Zhang ping", ' M ', 62.5},
{Fang, "he", "F", 92.5},
{Cheng, "ling", ' F ', 87},
{A, "Wang Ming", ' M ', 58},
};
Main ()
{
int i,c=0;
float ave,s=0;
for (i=0;i<5;i++)
{
S+=boy[i].score;
if (boy[i].score<60) c+=1;
}
printf ("s=%f\n", s);
AVE=S/5;
printf ("average=%f\ncount=%d\n", ave,c);
}
This example program defines an external structure array boy, a total of 5 elements, and initializes the assignment. In the main function, the score member value of each element is accumulated in s with a for statement, such as the value of score is less than 60 (fail) that is the counter C plus 1, after the cycle, calculate the average score, and output the whole class total score, average score and the number of failed.

[Example 7.5] set up a classmate's address book
#include "stdio.h"
#define NUM 3
struct MEM
{
Char name[20];
Char phone[10];
};
Main ()
{
struct MEM Man[num];
int i;
for (i=0;i<num;i++)
{
printf ("Input name:\n");
Gets (Man[i].name);
printf ("Input phone:\n");
Gets (Man[i].phone);
}
printf ("name\t\t\tphone\n\n");
for (i=0;i<num;i++)
printf ("%s\t\t\t%s\n", Man[i].name,man[i].phone);
}
This program defines a structure mem, which has two member name and phone used to represent names and phone numbers. The man is defined in the main function as an array of structures with the mem type. In the For statement, use the gets function to enter the values of two members of each element, respectively. The two member values in each element are then output in the For statement with a printf statement.

Structure pointer variable

The description of the structure pointer variable and the use of a pointer variable, when used to point to a struct variable, is called a struct pointer variable.
The value in the structure pointer variable is the first address of the struct variable to which you are pointing. The struct variable is accessed through the structure pointer, which is the same as the array pointer and function pointer. The general form of the description of the structure pointer variable is:
struct structure name * structure pointer variable name
For example, in the preceding example 7.1, the structure of Stu is defined, such as to describe a pointer variable pstu pointing to Stu, which can be written as:
struct Stu *pstu;

Of course, you can also specify Pstu when defining a STU structure. As with the various pointer variables discussed earlier, the struct pointer variable must also be assigned before it can be used. Assignment is to assign the first address of a struct variable to the pointer variable, and not to give the struct name to the pointer variable. If the boy is a struct variable that is described as a Stu type, then: Pstu=&boy is correct, and: Pstu=&stu is wrong.

The structure name and structure variable are two different concepts and cannot be confused. A struct name can only represent a single structural form, and the system does not allocate memory space to it. The variable is allocated storage space only if it is described as a struct of this type. So it's wrong to &stu this way, and it's impossible to get the first address of a structure name. With the structure pointer variable, it is easier to access the individual members of the struct variable.

The general form of its access is: (* structure pointer variable). member name or:
struct pointer variable-> member name
For example: (*pstu). Num OR: Pstu->num
It should be noted that the brackets on both sides of the (*pstu) are not small, because the member character "." Priority is higher than "*". If you remove the bracket writing *pstu.num is equivalent to * (Pstu.num), so the meaning is completely wrong. The following examples illustrate the specific description of the structure pointer variable and the use method.
[Example 7.6]
struct STU
{
int num;
Char *name;
char sex;
Float score;
boy1={102, "Zhang ping", ' M ', 78.5},*pstu;
Main ()
{
pstu=&boy1;
printf ("number=%d\nname=%s\n", boy1.num,boy1.name);
printf ("sex=%c\nscore=%f\n\n", Boy1.sex,boy1.score);
printf ("number=%d\nname=%s\n", (*pstu). Num, (*pstu). Name);
printf ("sex=%c\nscore=%f\n\n", (*pstu). Sex, (*pstu). Score);
printf ("number=%d\nname=%s\n", pstu->num,pstu->name);
printf ("sex=%c\nscore=%f\n\n", Pstu->sex,pstu->score);
}

This example program defines a struct Stu, defines the STU type structure variable boy1 and initializes the assignment, and defines a pointer variable pstu that points to the Stu type structure. In the main function, Pstu is given the address of boy1, so pstu points to boy1. The individual member values of the boy1 are then exported in three forms within the printf statement. From the results of the operation can be seen:
Structure variables. Member name
(* structure pointer variable). Member name
struct pointer variable-> member name

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.