[C basis] data type-structure and Union

Source: Internet
Author: User
1. Structure aggregation data types can store more than one individual data simultaneously. C provides two types of aggregate data: Array and StructureAn array is a set of elements of the same type. Each element of an array is selected through subscript reference or indirect access by a pointer. Different types of values can be stored in the structure. These values are members and members are accessed by name.
Structure declaration eg.1: struct simple {int A; char B; float C ;}; struct simple X, Y [20], * z;

Eg.2: typedef struct {int A; char B; float C;} simple; simple X, Y [20], * z;
Access structure member: eg: X. A; // direct access. X is the structure name.
Z-> A; // indirect access. Z is a pointer to the structure.
Structure Initialization is similar to array initialization.
Structure Storage Allocation: the compiler allocates memory to each member in the order of member lists. Only when the storage member needs to meet correct boundary requirements, the extra memory space used for filling can only appear before a member.
Generally, as a function parameter structure, the parameter should be set as a structure pointer. If the structure member is not changed, the keyword const is used. Eg: void F (register simple const * z) II. the joint statement and structure are similar, but the behavior is different from the structure. All associated members reference the same location in the memory.
The Union length is the length of its longest member. When the length of a member is large, it is better to store pointers to different members in the Union rather than directly storing the members themselves. (First save the pointer, and then dynamically allocate memory to save memory space .)
Initialization: it must be the first member type of the Union and must be in curly brackets. Eg: Unio {INTA; floata; charc [4];} X = {5}; // X. A is initialized to 5
Iii. Summary

In the structure (struct), different types of values can be stored together. The values in the structure are called members and they are accessed by name. A structure variable is a scalar that can appear in any common scalar.


The structure Declaration lists the members contained in the structure. Different Structure declarations are considered to be of different types even if their member lists are the same. A structure tag is a name associated with a member list. You can use structure labels to create Structure Variables of the same type in Different declarations, so that you do not need to repeat the member list in the declaration each time. Typedef can also be used to achieve this goal.


The structure member can make scalar, array or pointer. The structure can also contain members of the structure. Similar members in different structures will not cause conflicts. You can use the dot operator to access the members of the structure variable. If you have a pointer to the structure, you can use the arrow operator to access the members of the structure.


The structure cannot contain a type that is also a member of this structure, but its member can be a pointer to this structure. This technique is often used in chained data structures. To declare two structures, each structure contains a member pointing to each other's pointer. We need to define a structure tag name using an incomplete declaration. Structure variables can be initialized with a list of values surrounded by curly brackets. The value type must be applicable to the Members it initializes.


The compiler allocates memory to members of a structure variable to meet their boundary alignment requirements. Some memory space may be wasted when the boundary alignment of structured storage is implemented. Structure members can be sorted in descending order according to boundary alignment requirements to minimize the memory space wasted in structured storage. The value returned by sizeof contains the memory space wasted in the structure.


The structure can be passed to the function as a parameter or return from the function as a return value. However, a pointer to a structure passed to a function is often more efficient. You can add the const keyword to the declaration of the structure pointer to prevent the function from modifying the structure pointed to by the pointer.


A bit segment is a type of structure, but its member length is specified in bits. The bit declaration is essentially not portable because it involves many implementation-related factors. However, bit segments allow you to wrap values with an odd number in length to save storage space. If the source code needs to access any bit inside a value, it is easier to use the bit segment.


All members of a union are stored in the same memory location. By accessing different types of Federated members, the same bit combination in the memory can be interpreted as unnecessary. Union is useful in implementing variant records, but the programmer must be responsible for verifying which variant is actually stored and selecting the correct Union idiom for data access. Joint variables can also be initialized, but the initialization value must match the type of the Union 1st members, and it must be in a pair of curly braces. Storing pointers to different members in the Union instead of directly storing the members can solve the memory waste problem.


It is inefficient to transmit structure parameters by using pointers to the structure. Place the struct tag declaration and structure typedef declaration in the header file. When the source file requires these declarations, you can use the # include command to include them. Declare the bits as signed Int or unsigned Int.

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.