C Language Structural body-struct

Source: Internet
Author: User

Knowledge Points:

1) definition of the structure.

2) The structure of sizeof.

3) A pointer to the struct body.

1) Definition of structure:

A data structure that logically has a number of associated data types that act as a whole, and whose keywords are structs. Below I will define a struct body

struct student{

Char *name;

int age;

int SID;

};

I use the struct student defined above to define a variable.

struct Student Student;

The code above is a little annoying, in fact I can write this

struct student{

Char *name;

int age;

int SID;

} student;

or a typedef to define an alias for it.

typedef struct student{

Char *name;

int age;

int SID;

} Student;

Student Student;

Now that I have defined a student struct variable, it will be assigned the following value:

student={"Lishi", 23,1001};

Of course, I can also use a dot to assign values to the members in it:

Student.name= "Lishi"; student.age=23;student.sid=1001;

2) The structure of sizeof.

The structure I defined above student the number of bytes on a 64-bit machine, is not the sum of bytes occupied by all members, the answer is certainly not, of course, there is a coincidence, if the members are the same type of data. Its size should be greater than or equal to the sum of all members. Here is a concept of memory alignment that is not covered here. When we define a struct, its default alignment model size is the most byte-consuming type among its members, such as the above student, where the character pointer occupies the maximum number of bytes, which is 8 bytes. So the number of bytes occupied by student is an integer multiple of 8. However, one would ask, can we change this default alignment size? Of course, we can tell the compiler by #pragma pack (n), we want to specify the alignment size, where N is the exponent of 2. Here you have to point out, not how big you want to change it, if N is greater than the type that occupies the most of the word a in the struct, the alignment size will be the type that takes up the most bytes. I summarize the following points

A. struct size must be an integer multiple of "alignment size"
B. The starting address of each member must be an integer multiple of "self-aligning size"
C. The alignment size is determined by the type of itself and the maximum member type and pack (N)
D.N is the index of 2.

One thing to note here is that I define an array in the struct, the alignment size is not equal to the size of the array, of course not, this is only related to the type of the array.

3) A pointer to the struct body.

I'll define a struct pointer below, assuming we've defined an alias student for it.

Student *std;

So I can access its members by using the-I operator, for example: Std->name= "Lishi";

It is also important to note that a struct variable can be assigned to another struct variable whose name is just the equivalent of defining a name for the requested memory space, as in the case of the int type definition variable, and does not want the array name to be the address of the first element to it.

C Language Structural body-struct

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.