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

Source: Internet
Author: User
Tags definition integer

Structure type definition and structure variable description

In practical problems, a set of data often has different data types. For example, in the Student registration form, the name should be a character type, the number may be integral or character, the age should be integer, the gender should be a character type, and the grade may be integral or real. Obviously, you can't use an array to hold this set of data. Because the types and lengths of the elements in an array must be consistent for the compilation system to handle. To solve this problem, the C language gives another type of construction data-"structure". It corresponds to records in other advanced languages.

A struct is a type of construction that consists of several "members". Each member can be a basic data type or a constructed type. A struct is both a "constructed" data type, so it must be defined before it is described and used, that is, constructed. Just as you would define a function before describing and calling the function.

I. DEFINITION of structure

The general form of defining a structure is:
struct structure name
{
Member table columns
};
A member table consists of several members, each of which is an integral part of the structure. A type description must also be made for each member in the form of:
Type descriptor member name;
The name of the member should conform to the writing rules of the identifier. For example:
struct STU
{
int num;
Char name[20];
char sex;
Float score;
};
In this structure definition, the structure is named Stu, which consists of 4 members. The first member is num, the integer variable, the second member is name, the character array, the third member is sex, the character variable, the fourth member is score, the real variable. It should be noted that the semicolon after parentheses is not small. After the structure is defined, the variable description can be made. The variables described as structural Stu are made up of the 4 members mentioned above. This shows that the structure is a complex data type, is a fixed number of different types of a number of ordered variables set.

Description of structural type variables

The following three methods are used to illustrate structural variables. Take the Stu defined above as an example to illustrate.
1. Define the structure first, then describe the structure variables. Such as:
struct STU
{
int num;
Char name[20];
char sex;
Float score;
};
struct Stu Boy1,boy2;
The two variables boy1 and boy2 are described as Stu struct types. You can also use a macro definition to make a symbolic constant to represent a struct type, for example:
#define STU struct STU
STU
{
int num;
Char name[20];
char sex;
Float score;
};
STU Boy1,boy2;

2. Describe the structure variables while defining the type of structure. For example:
struct STU
{
int num;
Char name[20];
char sex;
Float score;
}boy1,boy2;

3. Direct description of structural variables. For example:
struct
{
int num;
Char name[20];
char sex;
Float score;
}boy1,boy2;

The third method differs from the second method in that the structure name is omitted from the third method, and the structure variable is given directly. The boy1,boy2 variables described in the three methods have the structure shown in Figure 7.1. After you have explained that the Boy1,boy2 variable is a Stu type, you can assign values to each member of the two variables. In the above Stu structure definition, all members are basic data types or array types. A member can also be a struct, which constitutes a nested structure. For example, Figure 7.2 shows another data structure. The following structure definitions can be given in Figure 7.2:
struct date{
int month;
int day;
int year;
}
struct{
int num;
Char name[20];
char sex;
struct date birthday;
Float score;
}boy1,boy2;
First, define a structure date, consisting of three members of month (month), Day (days), year (years). When you define and describe variable boy1 and boy2, the member birthday is described as the data structure type. Member names can have the same name as other variables in the program and do not interfere with each other. The representation method of a struct variable member is often not used as a whole when using structural variables in a program.

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.