Structure of the C language, and structure of the C Language

Source: Internet
Author: User

Structure of the C language, and structure of the C Language
Struct

A struct is a data structure. Int and float are basic data structures. Array is also a data structure, but the data type stored in the array is relatively simple. The structure solves the defect that arrays cannot store multiple types of data.

1. Structure reputation

It is only known as a data type, and space is allocated only when it is defined.

// Struct: the struct must exist. infor is the type name of the struct, which can be customized. Struct infor {char a [10]; float B; int c ;};
2. struct variable definition
Struct infor stu; // struct is also required for definition. Struct infor2 {char a [5]; float B; int c ;}a, B [3], * c; // variables can be defined while declared. // There is no name in the name struct, and it cannot be called after the name. Only variables can be defined at the same time, but no error will be reported during compilation. Struct {int a; float B; char c;} s; // You can rename the type with typedef to facilitate variable definition. // typdef is used for declaration, you can also use typedef struct {int a; float B; char c ;}s, * p_S; // S to indicate the new type name of the struct without writing the variable name, p_S is the type name S a; p_S p;
3. Initialization of struct Variables
Struct infor {char a [50]; char B; int c ;}; struct infor s1 = {"abc", 1}; // assign values in the member order during initialization, fill in 0 for incomplete initialization. // a [0] = 'A', a [1] = 'B', a [2] = 'C', and the rest are 0; struct infor s2 = {. a = "qew ",. B = 15}; // initialize struct infor s3 separately = {. a = 27 ,. a = "qew"}; // when initializing separately, the latter can overwrite the former. Struct infor * p_s = & s1;
4. Assignment and call of struct members

Common struct variables use the '.' operator to retrieve members. You must use the '->' operator to retrieve a Member from a struct pointer (similar to unreferencing ).

Struct infor {char a [50]; char B; int c ;}; struct infor s1 = {"abc", 1}; struct infor s2 = {. a = 27 ,. a = "qew"}; struct infor * p_s = & s1; // assign values to members after initialization. S1. B = 18; p_stu-> c = 16;
5. Structure memory alignment Principle

The starting address of the struct is an integer multiple of 4 by default. When the size of the largest memory member in the structure is smaller than 4, the starting address is an integer multiple of the size.

That is, when the largest memory member in the structure is short,

The start address is an integer multiple of 2 (2 <4, SO 2 is used ). When the largest memory member in the structure is double,

The start address is an integer multiple of 4 (8> 4, so the default value is 4 ). The first address of each member is an integer multiple of the memory occupied by the Member, so there is a memory filling principle.

Struct str1 // 7 bytes of valid data {int a; // 4, int type from the address is a multiple of 4 to store char B; // 1 // 1 fills short c with a 1-byte space; // 2. The short type is stored from a multiple of address 2} s1; // 8 struct str2 {char a; // 1 char B; // 1 // + 2 is filled with 2 bytes of int c; // 4} s2; // 8 struct str3 // 6 {int a; char B; char c; // + 2 fills in 2 bytes} s3; // 8 struct str4 // 10 {struct str1 s; // 8 char a; // 1 + 3 fills in 3 bytes Because struct str1 has the int type, so we need to add 4 bytes .} S4; // 12 struct str5 // 10 {short a [4]; // 8 char B; // 1 + 1 fills in 1 byte space} s4; // 10

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