C language training chapter 3 - structure

Table of contents

foreword

3.1 The meaning and syntax of the structure

3.1.1 Structure meaning

3.1.2 Structure syntax

3.1.2 Creation and initialization of structure variables

3.2 Types of structure members

3.3 Member access of structure

3.3.1. Operators

3.3.2 -> Operators

3.4 Structure parameter passing


foreword

Hello everyone! Welcome to continue learning c language with Cai Caijiang! Cai Caijiang had something to do before, so I was delayed, so let’s not talk nonsense, let’s start today’s study together!

3.1 The meaning and syntax of the structure

3.1.1 Structure meaning

A structure is a collection of values ​​called member variables. Each member of a structure variable can be a variable of different types. How can this be understood? Do you remember the meaning of an array? An array is a collection of the same type, but a structure is a collection of different types! So why use a struct? This is because built-in variables cannot describe complex situations . To describe a person in life requires age, gender, name, etc.! Let's take a look at the grammatical form of the structure!

3.1.2 Structure syntax

struct tag
{
   member-list;
}varaible-list;

 Explanation: tags can be named arbitrarily, and struct tags are combined into structure tags. The member-list is a member variable and is generally used to describe the related properties of the structure object, and the varaible-list is a global variable. This whole collectively makes up the struct type. Check it out with code!

struct stu
{
	int age;
	char name[20];
	char sex[5];
};

3.1.2 Creation and initialization of structure variables

Let's take describing one as an example to see how to create variables!

struct stu
{
	int age;
	char name[10];
	char sex[5];
}s2;//第二种方式
struct stu s1;//第一种方式
int main()
{
   //第3种方式
	struct stu s3;
	return 0;
}

Now the variable has been created, but we will find that this type is cumbersome to write. Is there any way to simplify it? Do you still remember typedef? typdedef can rename the type,

typedef unsigned int unit

 This code means to rename unsigned int to unit, but the renaming of structure is not written in this way!

typedef struct stu
{
   member-list;
}stu;

 The renaming of the structure by typedef is to put the renaming part after the braces and before the semicolon! Note that stu is no longer a list variable but renamed at this time! At this time, to write the structure type, you need to write stu instead of writing struct! There is no typedef for the structure in the c language, and the struct keyword cannot be omitted

How to initialize it? Remember how arrays are initialized? That's right, using curly braces, the initialization of the structure also uses the structure ! Let's take a look at the code

struct stu
{
	int age;
	char name[10];
	char sex[5];
};
int main()
{
	struct stu s = { 20,'张三','男'};
	return 0;
}

3.2 Types of structure members

The type of a structure member can be a scalar, an array, a pointer, or even a structure!

struct stu
{
	float price;
	char name[20];
	struct stu1;
	int* p;
};
struct stu1
{
	int age;
	int* pa;
	int name[10];
};
int main()
{
	struct stu s1;
    struct stu1 s;
    return 0;

}

The above code covers all possible cases. But if the member variable of the structure is a structure, how should it be initialized at this time? It's still the same as before Oh, let's see

struct stu
{
	float price;
	char arr[20];
	struct stu1;
	int* p;
};
struct stu1
{
	int age;
	int* p;
	char arr1[10];
};
int main()
{
	struct stu s = { 80.2,'张三',{20,NULL,"bit"},NULL };
	return 0;
}

3.3 Member access of structure

When we first met the C language, we introduced the structure member access operator. And ->, now let us take a closer look at it

3.3.1. Operators

How to use it, just look at the code

struct stu
{
	int age;
	char sex[5];
	char name[10];
};
int main()
{
	struct stu s1 = { 20,"男","张三" };
	printf("%d %s %s", s1.age, s1.sex, s1.name);
	return 0;
}

 To sum up. The specific use of operators: structure type. structure members

3.3.2 -> Operators

Remember the previous introduction? ->Applicable to structure pointers, let’s take a look at the code again! Still take the example of introducing a person's age and name, but the difference is to use a function to initialize and print it! upper code

#include<string.h>
struct stu
{
	int age;
	char name[10];
};
void set_stu(struct stu t)
{
	t.age = 20;
	strcpy(t.name, "张三");
}
void print(struct stu t)
{
	printf("%d %s", t.age, t.name);
}
int main()
{
	struct stu s1 = {0};
	set_stu(s1); 
	print(s1);
	return 0;
}

This is the solution we immediately thought of. At first glance, it seems that there is nothing wrong with it, but we found the problem when it was running.

 Why is 0 printed out? This is because the call by value is used when the function is called. At this time, the formal parameter is a temporary copy of the actual parameter, and the modification of the formal parameter will not affect the actual parameter. At this time, we should not call by value but call by reference, passing the address of the structure

struct stu
{
	int age;
	char name[10];
};
void set_stu(struct stu* t)
{
	//(*t).age = 20;
	//strcpy((*t).name, "张三");
    t->age=20;
    strcpy(t->name,"张三");
}
void print(struct stu t)
{
	printf("%d %s", t.age, t.name);
}
int main()
{
	struct stu s1 = { 0 };
	set_stu(&s1);
	print(s1);
	return 0;
}

 At this point you can print successfully

To sum up: structure pointer -> structure member

3.4 Structure parameter passing

In the above study, we can find that there are two types of structure parameter passing, passing by value and passing by address! upper code

struct stu
{
	char name[10];
	char sex[4];
	int age;
	int weight;
};
void print(struct stu s1)
{
	printf("%s %s %d %d\n", s1.name, s1.sex, s1.age, s1.weight);
}
void set(struct stu* s2)
{
	strcpy(s2->name, "张三");
	strcpy(s2->sex, "男");
	s2->age = 23;
	s2->weight = 200;
}
int main()
{
	struct stu s1 = { "张三","男",26,140 };
	print(s1);//传值
	struct stu s2 = { 0 };
	set(&s2);//传地址
	printf("%s %s %d %d\n", s2.name, s2.sex, s2.age, s2.weight);
	return 0;
}

Use the . operator when passing by value, and the -> operator when passing by address! There are two options for structure parameter passing, which is more appropriate? The answer is that it is better to use the address!

reason:

  • The function needs to push the stack when passing parameters
  • If the structure is too large when passing the next structure object, the system overhead of pushing the parameters on the stack is relatively large, which will lead to a decrease in performance

in conclusion:

When the structure is passed as a parameter, the address of the structure must be passed


The content of more structures will be introduced later in the study, and today's study ends here!

Guess you like

Origin blog.csdn.net/2301_77886098/article/details/131796348