Talking about structure and ->

Structure

reason

  Take student information management as an example. In the actual development process, if you just define a student’s information, you can apply for several variables for storage, but when you need to record a class, if you also use a variable corresponding to a person’s information, There will be a lot of code written repeatedly, so the structure is born, which can solve this problem well.

Declaration and initialization

statement

typedef struct Stu
{
	char name[20];
	int age;
	char sex[5]
	char id[20];
}Stu;

  In the structure, the member variables of the structure can be scalars, arrays, pointers and other structures.

initialization

struct Stu s = {"zhangsan", 20};

or

struct Node
{
	int data;
	struct Point p;
	struct Node* next; 
}n1 = {10, {4,5}, NULL};

Structure parameter

  When a function passes parameters, the parameters need to be pushed onto the stack. If a structure object is passed, the parameters of the structure are passed. Because the variables defined in the structure will be more and larger, and the parameters are passed It is to copy and transfer the value of the parameter, and there will be a lot of waste of space after multiple use, so when the structure is passed, the address of the structure must be passed.

Structure call

struct S
{
	int a;
	int b;
};
int main()
{
	struct S a, *p = &a;
	a.a = 99;
	printf("%d\n", (*p).a);
	printf("%d\n", a.a);
	printf("%d\n", p->a);

	return 0;
}

->

  The pointer used in the structure to point to the sub-data of the structure is used to fetch the sub-data. In layman's terms, if we define a structure in the C language and then declare a pointer to this structure, then we need to use a pointer To retrieve the data in the structure, use -> at this time.

Guess you like

Origin blog.csdn.net/weixin_43580319/article/details/111504472