C Language Structure
I. struct Definition
1. Definition form
Struct structure name
{Member list;}; // defined as a statement. The semicolon cannot be lost.
2. Declaration of structure type variables
(1) Declaration Form 1
Struct student
{
Int number;
Char name [10];
Float score;
};
Struct student st1, st2, st3;
(2) Declaration Form 2
Struct student
{Int number;
Char name [10];
Float score;
} St1, st2, st3;
(3) assign values to struct Variables
For example:
Struct student
{Int number;
Char name [10];
Float score;
} St1 = {101, "wang", 87.5 };
3. structure array Definition
Struct student
{
Int number;
Char name [10];
Float score;
} Stu [50] = {
{101, "wang", 87.5 },
{102, "li", 90 },
...
}; // Defines and initializes 50 array elements. Each element has a structure of student.
Ii. Structure pointer
1. Declaration Form
Struct structure name * pointer variable name // Method 1: the struct must be described before declaring the structure pointer variable
Struct structure name
{
Struct
} * Pointer variable name; // Method 2: Same as struct variable
2. Structure pointer assignment
Struct student {
...
};
Struct student * p;
Struct student st1 = {..};
P = & st1; // obtain the first address of the struct variable
3. Access Structure Variable members through structure pointer Variables
If st1 is the aforementioned student struct variable, p is the struct pointer variable:
To make p = & st1;, you can use the following methods to access each member of st1:
P-> num, p-> name, p-> score.
Or (* p). num, (* p). name...
Directly referenced with st1: st1.num can be used to represent members of st1.
4. pointer to the structure array
Struct student
{
Int num;
Char name [10];
} Stu [10] = {...}; // declare and initialize the structure array stu
Struct student * p; // declare the structure pointer p
P = stu; // assign a value to p, pointing to the first address of the array stu
For (; p
Printf ("% d, % s", p-> num, p-> name [10]); // reference a member of a structure array element through a pointer variable
Iii. Dynamic Storage Allocation
1. malloc Function
Allocate a N-byte continuous area in the memory dynamic storage area. The return value is the first address in the memory area. The type description indicates the Data Type stored in the memory area, (type specifier *) indicates to forcibly convert the returned value pointer to this type pointer:
(Type Description *) malloc (N)
For example:
(Int *) malloc (100 );
(Struct student *) malloc (sizeof (struct student ));
2. calloc Function
(Type specifier *) calloc (n, N) // The dynamic storage area is allocated with n consecutive regions with a length of N Bytes. the return value is the first address of the region. (type specifier *) indicates to forcibly convert the returned value pointer to this type of pointer;
3. free Functions
Free (void * ptr); // release the memory space pointed to by the pointer variable ptr. before using the free function, ptr must point to the Region allocated through malloc or calloc in advance.
Iv. Linked List
How do I allocate space to store student data? I can think of using struct arrays, but how can I determine the number of students? What should I do if I add or delete students? Extended chain table description:
Each element in the linked list is first a struct, and each element is called a node. Each node corresponds to a member in the structure as a pointer variable and points to the first address of the next node of the node, the first node is called a header node. There are only pointer variables that point to 0th nodes. the pointer variable Member of the last node points to NULL ).
For example, create a linked table structure for student data.
Struct student {char * name; struct student * pnext; // pointer variable pointing to the next node. The data type of the next node is struct}; // complete the int I Statement of the linked list structure; struct student * head, * p1, * pass; for (I = 0; I
Next = p1; pass = p1;} // use pass to store the current address, and pass printf ("input name: \ n") as the intermediate amount; scanf ("% s ", p1-> name); // enter p1-> next = Null for the content of the link node; // The current distribution area of each loop is the last node, so that the Pointer Points to Null} return head; // return the head node of the linked list