C language-structure study notes_dry goods!

Today, I read a sentence and share it with everyone: Yue Fei will return, so why bother with twelve gold medals in vain! Do what you need to do without delay.

Alright, go to the topic:

C language structure type-you can create your own compound data type.

1. Declare the structure type:

struct student_date
	{
		char name[NAME_LEN + 1];
		int age;
		int grade;
	};//这里的 ; 不要丢掉!!!

The name, age, and grade are all members of the student_date structure.

struct student_date liming; //定义一个变量liming,类型为student_date

	strncpy(liming.name,"liming",sizeof(liming.name) - 1);
	liming.name[sizeof(liming.name) - 1] = '\0';//其实如果liming.name足够长直接用strcpy就可以了

	liming.age = 18;
	liming.grade = 1;

note:

In addition to initial value assignment, character array assignment cannot use = assignment, so here we use strcpy/strncpy to assign it!

The character pointer can be assigned with = at any time!
 

You can also define this structure variable directly when you declare the structure (more commonly used):

struct student_date
	{
		char name[NAME_LEN + 1];
		int age;
		int grade;
	}liming,xiaohong,Jane;//这里的 ; 不要丢掉!!!

 

2. Regarding the structure declaration inside/outside the (main) function:

1>. The structure is the same as the local variable, the structure type declared inside the function can only be used inside the function

2>. So usually the structure type is declared outside the function, so that it can be used by multiple functions

3.struct unnamed structure (I understand this structure as a one-time definition of structural variables)

struct{
    int x;
    int y;
}p1,p2;

4. The structure can be initialized with {}

struct student_date liming = {"liming",18,1};

struct student_date Kangkang = {.name = "Kangkang",.grade = 1};//这里发现少了个age,没有初始化就会认为是0

5. Structure members (may not be the same type, you can see it from the above!)

Use the operator. to access the members of the structure

6. Structural calculations

1>To access the entire structure, use the name of the structure directly

2>As for the whole structure, you can do the assignment to get the address, or pass the parameter to the function.

Such as:

	struct student_date
	{
		char name[NAME_LEN + 1];
		int age;
		int grade;
	}liming;//这里的 ; 不要丢掉!!!
	
	liming = (struct student_date){"liming",18,1};

	struct student_date liming2;

	liming2 = liming;//赋值。而数组只能在初始化时赋初值,不可以后续做赋值运算 如: a[2] = {3};×

3>The name of the structure variable is not an address, it needs to take the address character &

	struct student_date *pliming = &liming;
	printf("%s\n",(*pliming).name);

 Here first declares a pointer to the structure (structure pointer)

Structure pointers can access structure members: (*point).member;

Structure pointers can be used as function parameters (low overhead)

7. As a function parameter

For example, int f (struct student_date liming);

1>At this time, the entire structure will be passed into the function as a parameter, the process is to create a new structure variable in the function, and copy the value of the structure again

2> can return a structure

3>For the input of the structure:

Scanf cannot input a structure at a time, but we can write a function of my_scanf to read a structure by ourselves, and then return a structure (but the overhead is high)

4>The structure pointer is used as a parameter:

A more efficient way for a large structure is to pass pointers! (As mentioned earlier, whether it is passing a parameter into a structure or returning a structure, it is a copy of one, which is very expensive)

Create a structure pointer as follows:

struct date{
    int month;
    int day}myday;

struct date *p = &mydate;

Use the structure pointer to modify the value of the structure member:

(*p).month = 12;

p -> month = 12;

Both of these forms are possible, and the two are equivalent. (Note: -> indicates that the pointer points to the member in the structure variable)

5>A small routine commonly used:

struct point{
    int x;
    int y;
};//定义了一个 点 结构(x,y)
struct point* getstruct (struct point *p)//函数getstruct传入的是指针,返回的也是指针
{
    scanf("%d",&p->x);
    scanf("%d",&p->y);

    return p;//返回指针
}

8. Structure in structure

1>

struct point{
    int x;
    int y;
};//表示一个点(x,y)
struct rectangle{
    struct point point1;
    struct point point2;
};//左上角一个点右下角一个点足以确定一个矩形(rectangle)

struct rectangle rectangle1;//定义了一个rectangle变量rectangel1

When you visit in this way, you will have:

rectangle1.point1.x

2>

If

struct rectangle rectangle1,*p_rectangle;//有一个rectangel1,还有一个指针p_rectangle
    p_rectangle = &rectangle1;//指针p_r指向r

Then p_rectangle -> point1.x is equivalent to rectangle1.point1, and x is equivalent to (p_rectangle->point1).x!

3>There are also arrays in the structure...Wait and combine by yourself!

 

 

 

Okay, good night~~

Guess you like

Origin blog.csdn.net/qq_51182221/article/details/115286032