C Language Basics Tutorial (iv) Pointers, structures, unions, and enumerations (9)

Source: Internet
Author: User

2. Structure (struct)

A structure is a combination of various variables that are composed of basic data types and named with an identifier. Different data types can be used in the structure.
1. Structure description and Structure variable definition
In Turbo C, a struct is also a data type, and you can use a struct variable, so, as with other types of variables, you define the structure variables first.
The general format for defining structure variables is:
struct structure name
{
The type variable name;
The type variable name;
...
} structure variables;
The structure name is the identifier of the struct, not the variable name.
Type is the five data types described in section II (integer, floating-point, character, pointer, and no value).
Each type variable of the constituent structure is called a struct member, and it is like an element of an array, but the elements in the array are accessed by the following object, and the struct accesses the member by the variable name.
Here's an example to illustrate how to define a struct variable.
struct string
{
Char Name[8];
int age;
Char sex[2];
Char depart[20];
Float Wage1, Wage2, Wage3, Wage4, Wage5;
} person;
This example defines a struct variable person with a struct named string, and if you omit the variable name person, it becomes a description of the structure. Structure variables can also be defined with the described structure name. The example above in this definition becomes:
struct string
{
Char Name[8];
int age;
Char sex[2];
Char depart[20];
Float Wage1, Wage2, Wage3, Wage4, Wage5;
};
struct string person;
If you need to define more than one structure variable with the same form, it is more convenient to use this method, it is a structure description, and then use the structure name to define the variable.
For example:
struct string tianyr, Liuqi, ...;
If the structure name is omitted, it is called the nameless structure, which often occurs inside the function, and the previous example in this structure becomes:
struct
{
Char Name[8];
int age;
Char sex[2];
Char depart[20];
Float Wage1, Wage2, Wage3, Wage4, Wage5;
} Tianyr, Liuqi;

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.