C and pointer (pointers on C) -- Chapter 10: Structure and Union (I)

Source: Internet
Author: User
Chapter 10 structure and Union
This section first introduces the. operator, which can be accessed directly. It also introduces the-> operator, which replaces the indirect access operation (* struct). xxx
It is not difficult for beginners to understand this chapter. If they have not learned the operating system, they may have some problems in understanding the concepts of bit segments and alignment.
It is more and more explained that pointer and memory are definitely the core of C.


Summary:
The structure Declaration lists the members contained in the structure. Different types of values can be stored together.
Different Structure declarations are considered to be of different types even if their member lists are the same.
It is a good method to use typedef to create a new type when declaring the structure.
Typedef struct
{
Int;
Char B;
Float C;
} Simple;
The results are almost the same as those of structure labels. The difference is that simple is now a type name rather than a struct, and the declaration in the future is like the following.
Simple X;
Simple y [20], * z;
The structure cannot contain a type that is also a member of this structure. Otherwise, an infinite number occurs during address allocation. However, it can contain a pointer to this structure.
This is often used in chained structures. To declare two structures, each structure contains a member pointing to the other party's pointer.
During initialization, a list of values surrounded by curly brackets can be used for initialization.
The compiler allocates memory to members of a structure variable to meet their boundary alignment requirements. Some space may be wasted when the boundary alignment of structure 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.
If you want to reduce the loss caused by alignment, the Declaration process should be sorted in descending order. You can use offstof macro (stddef. h) + sizeof to find a better solution.
The structure can be passed to the function as a form parameter, or returned as a return value, but it is absolutely not recommended !!! A serious waste of memory space.
It is best to use pointers for parameters and returned values. If you are afraid of modifying the structure itself, add a const!


The individual does not have a deep understanding of the bit segment. In terms of understanding, it is possible to package the values with an odd number in length to save space.


Union and struct are two different stories. All union members are stored in the same memory location. You can also perform initialization, but the initialization must match the first member type of the Union.
Initialization:
Union {
Int;
Float B;
Char C [4];
} X = {5 };
In this way, X. A is initially set to 5.


Warning:
1. Structure declarations with the same member list generate different types.
This is like a function, which has a lot to do with declaration.
2. Be careful when using typedef to define a name for a self-referenced structure.
You must remember to reference pointers for auto-reference.
3. Passing Structure Parameters to functions is inefficient.
It is a waste of space to copy the form parameters in the memory.


Programming tips:
1. Put the typedef declaration in the header file. # Include is used.
2. The optimal arrangement of structure members is not necessarily the arrangement that considers boundary alignment and wastes the least memory space.
Obviously, in order to align, sometimes you have to disrupt the declared order, resulting in reduced readability.
3. explicitly declare the bits to the signed Int or unsigned int type.
4. Bit segments cannot be restrained.
5. Bit segments make the expression in the source code clearer.


Problem:
1. What are the differences between members and array elements?
The members can have different types, but the array does not work.
2. What are the differences between structure names and array names?
The array name is a pointer constant. However, the structure name is a tag, which is similar to the class name. If there is no instance, no memory is allocated to it.
3. Structure declaration:
Struct tag {Member-list} variable-list;
Tag, member-list, and variable-list must have two.

C and pointer (pointers on C) -- Chapter 10: Structure and Union (I)

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.