Consortium and struct in C Language

Source: Internet
Author: User


Struct definition is very simple. Note that no space is allocated here.

// Here we only define a simple struct type and do not allocate the memory space struct student {int num; char name [20];};
Define struct variables:

struct student stu;

Of course, the simpler method is to use defined. I will not elaborate on it here, because the focus of this article is not here. Print the size of struct and internal variables below

Struct student stu; printf ("struct size-> % lu \ n", sizeof (stu )); // The result is 24 printf ("int size-> % lu \ n", sizeof (int )); // The result is 4 printf ("char [20] size-> % lu \ n", sizeof (char [20]); // The result is 20

Good 4 + 20 = 24, but if we add another variable sex in the struct:

struct student {    int num;    char sex;    char name[20];};

Variable Size in the next print:

    struct student stu;    printf("struct size->%lu\n", sizeof(stu)); //28    printf("int size->%lu\n", sizeof(int));  //4    printf("char size->%lu\n", sizeof(char)); //1    printf("char[20] size->%lu\n", sizeof(char[20])); 20

This time we will find that 4 + 1 + 20 is not equal to 28. If we change char name [20] To char name [23], the struct size is still 28, but if we change int num; it is defined between char sex; and char name [23:

struct student {    char name[23];    int num;    char sex;};

The struct size will change to 32. Why?

This is related to the memory management of the operating system. To facilitate memory management, the operating system manages the memory in a certain number of bytes. For example, the memory is measured in four bytes, that is to say, if a variable with less than 4-byte integers appears in struct, it will be allocated four-byte integers. If the next variable is still less than four-byte integers, it will "merge" with the above variable to share a four-byte value. Of course, the memory management design is not clear in one sentence. Here is a brief introduction. You can learn more deeply ......

As for union, a consortium or a shared body, the usage of union is very similar to that of struct. The only difference is that the Memory Length occupied by the struct variable is the sum of the memory of each member, the union memory length is the length of the largest member in the memory, that is, several member variables of the union share a piece of memory.

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: 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.