preface
C language provides different data types, such as int, float, double, char, etc. different types determine the space and expression of a variable in memory.
However, when we define a person, it is difficult to define different attributes of a person with the same data type, because people's height, age, weight and other attributes often need different data types. At this time, we introduce the concept of structure.
1, Declaration and definition of structure
1. Declaration of structure
A structure is a collection of values called member variables. Each member of the structure can be a different type of variable
When we face things with multiple different data types, we can use structures to organize them.
For example, a book has different data types such as book name, author, selling price, publication date, etc. at this time, we can create a structure to contain different data types of the book.
Structure declaration is the main method to describe structure combination. The syntax format is:
struct structure name
{
Structure member 1;
Structure member 2;
Structure member 3;
...
};// Semicolons cannot be lost
[note]
Structure members can be either any basic data type or another structure. If it is the latter, it is equivalent to the nesting of structures. (commonly known as dolls)
For example:
struct Book//Describes the properties of a Book, where Book is the name of the framework { char name[20];//title char author[20];//author float price;//Price };//The semicolon must not be lost
This is equivalent to describing the framework of a book.
2. Type of structure member
The types of structure members can be scalars, arrays, pointers, or even other structures.
3. Definition of structure
The declaration of a structure is just a simple description. In fact, it will not allocate space in memory until the structure type variable is defined.
In other words, it has not been really used. It exists virtually. It exists only when the structure type variable is defined.
For example, it defines the framework of the book
struct Book//Describes the properties of a Book, where Book is the name of the framework { char name[20];//title char author[20];//author float price;//Price };//The semicolon must not be lost
Here, in the compiler, memory space is not allocated, it is just a virtual existence. Once we define the structure type variable, it can be allocated space.
For example:
struct Book//Describes the properties of a Book, where Book is the name of the framework { char name[20];//title char author[20];//author float price;//Price };//The semicolon must not be lost int main() { struct Book book;//Local variables -- put in the stack area return 0; }
In the above example, we can also note that the syntax for defining structure variables is:
struct structure name structure variable name
In addition, structure variables can be defined when a structure is declared
struct Book//Describe the relevant attributes of a book { char name[20]; char author[20]; float price; }b1,b2;//B1 and B2 are global variables. Put in static area int main() { struct Book book;//Local variables -- put in the stack area return 0; }
Structural variables b1 and b2 are global variables, which can also be accessed in other functions.
2, Initialize structure
We can initialize a variable or array when defining it,
For example:
int a=10; int arr[10]={1,2,3,4,5,6,7,8,9,0};
Similarly, when defining a structure variable, we can initialize it at the same time
struct Book//Describe the relevant attributes of a book { char name[20]; char author[20]; float price; }b1,b2;//B1 and B2 are global variables. Put in static area int main() { struct Book book= { "<Xiaoao Jianghu","Jin Yong",30 };//In this way, the structure variable is initialized, that is, the variable is defined and the initial value is assigned at the same time return 0; }
3, Access structure members
Structural variables access members. Members of structural variables are accessed through the point operator (.). The point operator accepts two operands.
For example, book.name is the name member that references the book structure variable, which is a character array.
#include <stdio.h> struct Book//Describe the relevant attributes of a book { char name[20]; char author[20]; float price; }b1, b2;//B1 and B2 are global variables. Put in static area int main() { struct Book book= { "<Xiaoao Jianghu", "Jin Yong", 30 };//In this way, the structure variable is initialized, that is, the variable is defined and the initial value is assigned at the same time printf("%s %s %f\n", book.name, book.author, book.price); //Access with return 0; }
4, Structure nesting
If you access nested structure members, you need to use the multi-level dot operator. Because the structure of C language can only access the lowest level members, if there is multi-level structure nesting, it needs to go deep level by level until the lowest level members are found
struct S { int a; char c; double d; }; struct T { struct S s;//Structure nesting char name[20]; int num; }; int main() { struct T t = { {100,'c',3.14},"Reese",30 }; printf("%d %c %f %s %d\n", t.s.a, t.s.c, t.s.d, t.name, t.num);//A two-level dot operator is used to find members return 0; }
5, Structure pointer
At the beginning, we said that the members of a structure can be scalars, arrays and pointers.
Here, let's take a look at structure pointers.
struct Book *pt;
Here, a pointer variable pt pointing to the Book structure type is declared
struct S { int a; char c; double d; }; struct T { struct S s; char name[20]; int num; }; int main() { struct T t = { {100,'c',3.14},"Reese",30 }; printf("%d %c %f %s %d\n", t.s.a, t.s.c, t.s.d, t.name, t.num); struct T*pt = &t;//How to get the address printf("%d %c %f %s %d\n", (*pt).s.a, (*pt).s.c, (*pt).s.d, (*pt).name, (*pt).num); printf("%d %c %f %s %d\n",pt->s.a,pt->s.c,pt->s.d,pt->name,pt->num); return 0; }
[note] the array name refers to the address of the first element, so you can directly assign the array name to the pointer variable. However, the variable name of a structure variable does not point to the address of the structure, so you need to use the address operator (&) to get its address.
As above:
struct T*pt = &t;// How to get the address
Through the above example, we can also find that there are two methods to access structure members through structure pointers:
(1) (* structure pointer). Member name
(2) Structure pointer - > member name
First, because the dot operator (.) has higher priority than the value taking operator (*) of the pointer, you should use a small slogan to dereference the pointer first, make it become the variable of the structure, and then use the dot operator to access its members.
The above two methods are completely equivalent when implemented. However, remember that the dot (.) can only be used for structures and the arrow (- >) can only be used for structure pointers.
[the printing result is the same]
When both are available, the second method is preferred, because the arrow has directivity, which can be intuitively connected with the pointer.
6, Structural transmission parameters
When a function is called, the transfer of parameters is the process of value transfer, that is, the process of transferring arguments to formal parameters. Therefore, structural variables can be passed as function parameters, and two structural variables of the same structural type also support direct assignment.
struct S { int arr[100]; int num; char ch; double d; }; //Structural transmission parameters void print1(struct S ss) { printf("%d %d %d %c %1f", ss.arr[0],ss.arr[2],ss.num,ss.ch,ss.d); } //Structure address transfer parameter void print2(struct S*ps) { printf("%d %d %d %c %1f", ps->arr[0], ps->arr[2], ps->num, ps->ch, ps->d); } int main() { struct S s = { {1,2,3,4,5}, 100, 'w',3.14 }; print1(s);//Transmission structure print2(&s);//Transmission address return 0; }
You can see that the parameter is indeed passed.
So, which of the print1 and print2 functions above is better?
The answer is: print2 function is preferred. Reason:
When passing parameters to a function, parameters need to be stacked. If the structure is too large when passing a structure object, the system overhead of parameter stack pressing is relatively large, which will lead to performance degradation.
Therefore, when the structure passes parameters, the address of the structure should be passed.
summary
This article introduces some basic knowledge of C language structure. The content of structure is far more than that. After learning more content in the future, I may write a blog to introduce it in depth. In addition, this paper refers to the book "zero basic introduction to learning C language" by little turtle, as well as some materials on the Internet, and combed with his notes when learning and listening to the class. There may be some omissions, or the source of the content, or the omission of the explanation. Please also see that everyone contains and forgive me!I hope it can help you!