C Language _ Structure body

Source: Internet
Author: User

Concept of struct type:

A struct is a collection of data that contains a single number.

The C language provides a structured data type as a collection to handle this complex data, and a struct can combine multiple single information together as a whole to use. When using a struct, you first declare the type of struct you need, in general form Shi:

struct structure Body name {

Data type 1, struct member 1;

Data type 2, struct member 2;

Data type 3, struct member 3;

......

};

The structure body name is equivalent to the tag of this structure body, and it composes the name of the structure body together with the identifier struct. Each structure can contain many members, each of which has a data type that can be declared as any valid data type except for the structure body type itself.

To define a structural body variable:

Once you declare a struct type, you can use that type to define the structure body variable. There are three forms of structural body variables defined in C language, which is in fact the three forms of declaring struct types:

1. Declare the structure body type and define the structural body variable;

2. Do not declare the structure body type, directly define the structural body variable;

3. Use the typedef to obtain a simplified structure type name, and then define the structural body variables.

The byte length of the struct body:

In a general system, for the convenience of addressing, data is usually stored in memory with the byte length of its own data type aligned to the base unit. While the structure data is stored, it is aligned with the number of bytes in the member with the largest byte length.

The members in the structure are allocated contiguous memory space. The number of bytes in a struct variable, that is, the byte length of the structure data type, is generally equal to the byte length of the members of each structure body.

Justification means that the memory is divided into a fixed number of bytes as the smallest unit, allocating memory can only be allocated piece by piece.

However, when the length of the bytes in the adjacent structure member is not a moment, due to the effect of the memory alignment, the structure body occupies some extra space, so that its memory space is greater than the total byte length of the members of each structure body.

Use of the structure body:

To access a member of a structural body:

Members of a struct can be accessed using the member operator (.). The member operator is a two-dollar operator, the preceding operator is a struct variable, and the following operator is a struct member name. The standard form is as follows:

Structural body name.

Access to members can be achieved only through direct access to the members of the struct body.

To initialize a structural body variable:

With struct variables, as with other type variables, it is best to initialize them before use. The method for initializing a struct is similar to an array initialization, and you can assign values to a struct member sequentially by using a numeric sequence.

Structure Body Array:

An array of structural bodies is an array of elements of a struct type.

To define a structure body array:

There are three ways to define a structural body array, as opposed to defining a struct variable:

1. Declare the structure body type and define the structure array:

You can declare a struct type and then use a struct type to define an array of structures.

2. Do not declare struct type, directly define structure body array: You can also define the structure body array directly.

3. Use typedef: You can also use the typedef to get an alias for the struct type, and then use that alias to define the structure array.

Structure Body pointer:

A pointer to a struct variable: Defining a pointer to a struct variable is basically the same as defining a pointer method that points to other types of variables.

General structure variables can only be members by using the member operator, while the struct pointer is able to get members by arrow operators.

Structural body pointer to heap space:

You can define a struct pointer variable and point it to the memory space allocated from the heap.

A pointer to an element of the structure body array:

By assigning the address of an array element of a struct to a pointer variable, you can obtain a pointer variable that points to an array element of the structure body. If the address used to assign a value is an element of an array of structures, you can access each of the structural body elements in the array using the pointer variable and the mouth of the relative offset.

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.