Basic knowledge of structure
In c language, a struct is a data structure. A struct can be declared as a variable, a pointer or an array, and, at the same time, a collection of elements, called struct members, that can be of different types, with members typically accessed by name
struct Stu//stu as struct tag {char name[20]; int age; CHARSEX[5]; Char tele[12]; Char addr[30];//member}STU1,STU2;//STU1 and STU2 are global struct variables, int main () {struct Stu stu;//create struct variable}
Anonymous struct, the struct tag is omitted, you must declare the global variable later, but there are different types of problems
struct{Char name[20]; int age; CHARSEX[5]; Char tele[12]; Char addr[30];//member}stu1;struct{Char NAME[20]; int age; CHARSEX[5]; Char tele[12]; Char addr[30];//member}*pstu;//two struct member is identical to int main () {pstu=&stu1;/*error, two types are different because they are anonymous struct types, the variable compiler created by them will think they are different types */}
typedef simplifies structs
struct Stu {char name[20]; int age; CHARSEX[5]; Char tele[12]; Char addr[30];//member}stu1,arrstu[10],*pstu;typedef struct STU stu;/* if typedef is prepended to the struct, then the following global variable is represented as the new name of the struct */int main ( {stu stu2;//omit struct Stu arr2[10]; Stu *pstu; return 0;}
struct members
1. Direct Access
struct Stu {char name[20]; int age; Char sex[5]; Char tele[12]; Char addr[30];//member};typedef struct stu Stu;int main () {stu stu;//struct variable//stu.name= ' bit ';//error, cannot assign a value to a constant strcpy (stu.name , "AAA"), stu.age=10;printf ("name=%s", Stu.name);p rintf ("age=%d", stu.age); return 0;}
2. Indirect access
--for struct pointers
. Used for struct-body variables
struct Stu {char name[20]; int age; Char sex[5]; Char tele[12]; Char addr[30];//member};typedef struct stu* pstu;int main () {stu stu;//struct variable pstu pstu1=&stu;//strcpy ((*PSTU1). Name, " AAA ");//(*PSTU1). age=20;strcpy (Pstu1->name," AAA "); return 0;}
Self-citation of structural body
Example 1. The Wrong way
struct A{char name[10];int age;struct A sa;} int main () {struct A sa;}
Use recursion to create a cycle of death, and constantly quote yourself
Correct
struct A{char name[10];int age;struct a *sa;//size is an exact value, the pointer finds the next}int main () {struct A sa1;struct a sa2;struct a SA3;SA1.PA =&sa2;sa2.pa=&sa3;sa3.pa=null;}
If you want to overwrite with a typedef,
typedef struct A//struct nested inside the same type pointer, rename struct cannot be anonymous {char name[10];int age;struct A *sa;} A;int Main () {a sa1; a sa2; a sa3;sa1.pa=&sa2;sa2.pa=&sa3;sa3.pa=null;}
Incomplete declaration
struct A{int i;struct b b;}; struct B{int i;struct A a;};/ /Do not allow this type of writing, will always call each other down
Rewritten as
struct b;struct a{int i;struct B *b;}; struct B{int i;struct A *a;};
struct initialization
struct Stu{char name[10];int age;char sex[4];char tel[12];char add[20];}; int main () {struct Stu stu={"aaa", "male", "123", "Xia"};p rintf ("%s\n", Stu.name);p rintf ("%d\n", stu.age); return 0;}
Initialization of structures with struct bodies
struct a{int A; char c; double D;}; struct Stu{char name[10];int age;char sex[4];char tel[12];char add[20];struct A sa;}; int main () {struct Stu stu={"aaa", "male", "123", "Xian"};//not fully initialized struct Stu stu={"aaa", "male", "123", "Xian", {1, ' W ', the member before 12.34}};//SA cannot omit printf ("%s\n", Stu.name);p rintf ("%d\n", Stu.age);p rintf ("%1f\n", STU.SA.D); return 0;}
Structure size
struct S{int a;char c;couble D;}; struct S2{int a;double D;char c;}; int main () {printf ("%d\n", sizeof (struct s));p rintf ("%d\n", sizeof (struct S2)); return 0;} Output 16 24
Memory alignment rules exist
The first member is at an address that is offset to the struct variable at address 0
The address of the other member variable to be aligned to an integer multiple of a number (the number of alignments)
Alignment number = The compiler's default alignment number and small value for the member size
vs Default 8,linux Default 4
3. The total size of the structure is the maximum number of alignments (each member variable has an alignment number in addition to the first member).
4. If a struct is nested, the nested structure is aligned to the integer multiples of its maximum alignment number, and the overall size of the struct is an integer multiple of the maximum number of pairs (with the number of alignments of the nested structure)
struct a{double D;char C; Short S; Dpuble D2;}; int main () {printf ("%d", sizeof (struct A)),//24printf ("%d", offsetof (struct a,d));//0}
Offsetof is the address to which each type is asked
This article is from the "incomparable Warm yang" blog, please be sure to keep this source http://10797127.blog.51cto.com/10787127/1726525
Understanding the structural body