Function calls and nesting of structures

Table of contents

Structure definition and assignment methods

How to call a single structure

Structures of the same type can be assigned to each other:

Nesting of structures:

Function calling method of structure array:

Pass the first address of the structure array to the function:

Structure arrays and pointers


      This article mainly talks about the calling of structures and functions, which is divided into two parts. The first part is the calling of a single structure, and the second part is the calling method of the structure array;

Structure definition and assignment methods

Let’s briefly talk about the 5 ways to define structures

1.
struct stu s[3]   //在首部进行定义。
{
	int num;
	char name[30];
};//一定要注意这里的分号,定义结构体函数相当于是一条语句,需要以分号结尾。


2.
	struct stu
{
	int num;
	char name[30];
}s[3],*p;//在末尾和结构体同时定义,可定义多个变量用逗号分隔,注意是在分号里面。p为结构体指针。


3.
struct stu
{
	int num;
	char name[30];
};
int main()
{
	stu s[3];//可以在主函数中定义变量

4.
struct stu
{
	int num;
	char name[30];
};

struct stu s[3];//在函数外进行定义。这时就相当于是一个外部变量;和普通的外部变量一样。函数内可以定再次义重名变量


5.
struct           //甚至可以没有类型名,直接定义变量。
{
	int a;
	float b;
}stu={5,3.0}; //此处的stu为结构体变量名

但此种方式并不推荐,因为只能在此末尾定义好,在函数体中没办法定义。因为省去了结构体类型名。

stu is the structure type name , which is used to define structure variables in functions or define the types of formal parameters in function calls.

s[3] is a structure variable . is an array of structures.

How to assign values ​​to structures

The first is to assign the initial value directly during definition . as follows;

struct stu
{
	int num;
	char name[30];
};
int main()
{
	stu s[3]={3,"zhangsna",5,"lisi",6,"wangwu"};//可以在定义时统一赋值。
    stu s[3]={
   
   {3,"zhangsna"},{5,"lisi"},{6,"wangwu"}};//这两种形式都是可以的。

The second method is to assign values ​​within a function . Assigning values ​​within a function follows the member types in the structure variables and assigns values ​​in sequence. Values ​​cannot be assigned at the same time .

#include<string.h>
int main()
{	
stu s[3];
scanf("%d%s",&s[0].num,s[0].name);//依次对应经行赋值赋值,该加取地址符的要加

s[1].num=2;//单独调出其中的成员进行赋值。

strcpy(s[1].name,"laowang");//字符串,要遵循字符串的赋值规则。

s[2]={3,"laoliu"};//这种形式是不正确的!!!

How to call a single structure

It is relatively simple to directly pass the structure element or element address.

        If the address of an element is passed, does the pointer received by the function need to be defined as a structure type? The answer is no, you just need to define a pointer consistent with the element type. Because what is passed is only an element, not a structure. The structure pointer should point to the address of the structure variable;

A comprehensive example is listed, including the types passed and the types received:

#include<stdio.h>
 struct stu//定义结构体类型名 stu;
{
	char name[20];
	int num;
	float fen;
 }; 
void fun(char *q,float n,stu *p)//定义了三种接收类型;字符型指针,实型变量,结构体指针;
 {
 	n=36.6;
 p->num=3;
 printf("这是%s\n",q);
 }
int main()
{
	stu s={"wang",1,30.54,},*p;//定义结构体变量s和结构体指针p;
	p=&s;
	printf("改变前:%f,%d\n",s.fen,s.num);
    fun(s.name,p->fen,&s); //三种传递方式
    printf("改变后:%f,%d",s.fen,s.num);
	return 0;
}

Elements can be transferred directly, in the same way as ordinary variable transfer, except that the element form of the structure is:

    The name of the structure variable.Member name

The pointer method is:

(*pointer).Member name pointer->member name

fen passes a value, so the actual value will not be changed in the sub-function, while num passes an address, so the actual value will be changed.

And if a pointer is passed, the received type must be a pointer of a structure type;

void fun(stu *p)//用一个结构体类型指针来接收;
 {
 p->num=5;
 }
int main()
{
	stu s={"wang",1,30.54,},*p;
	p=&s;
	printf("改变前:%d\n",s.num);
       fun(p); 
    printf("改变后:%d",s.num);
	return 0;
}

      This also passes the address, because p points to the address of the structure variable s; so the sub-function can also change the actual parameter value;

Structures of the same type can be assigned to each other:

 struct stu//定义结构体类型名 stu;
{
	char name[20];
	int num;
	float fen;
 }; 
int main()
{
	stu s={"wang",1,30.54,},s1;
   s1=s;
printf("%d",s1.num);

Nesting of structures:

 struct ST//定义内嵌套结构体类型ST;
 {
 	int a=5;
 	float b=10.5;
 };
 struct stu//定义主体结构体类型stu;
{
	char name[20];
	 ST   w;//定义ST类型结构体变量w;
 }; 
int main()
{
	stu s;
    printf("%d",s.w.a);//输出嵌套体中的变量a;
	return 0;
}

The nested body must be defined before the main body , otherwise the main structure will not know the defined type (it will be the same even if it is defined in the main function)

The nested body can have the same name as the main body, but it is best not to, as it is easy to get confused;

Function calling method of structure array:

int i;
stu s[3]={
   
   {"wang",1,30.54,},
	          {"liu",2,36.5},
			  {"li",3,40.6}},*p[3];//这时可以定义一个结构体指针数组与之对应;
   for(i=0;i<3;i++)	
    p[i]=&s[i];
  fun(s[1].name,p[1]->fen,&s[1]); 

        The structure array is similar to an array, but the way of obtaining values ​​is different from that of an array.

Pass the first address of the structure array to the function:

#include<stdio.h>
 struct stu
{
	char name[20];
	int num;
	float fen;
 }; 
void fun(stu a[])//用一个结构体数组来接收;
 {
 a[0].num=3;
 }
int main()
{
	stu s[3]={
   
   {"wang",1,30.54,},
	          {"liu",2,36.5},
			  {"li",3,40.6}};
	printf("改变前:%d\n",s[0].num);
       fun(s); //将结构体数组的首地址传过去;
    printf("改变后:%d",s[0].num);
	return 0;
}

 Because the array name points to the address of the first element of the array , it is equivalent to a pointer . Passing the name of the structure array directly is equivalent to passing the address of the first structure variable , and then using a structure array to receive it. The principle is the same as passing the array to the function . In this way, the subfunction can receive multiple structure variables, and the values ​​of the actual parameters can be changed in the subfunction.

Structure arrays and pointers

Determine the priority clearly, and then perform the operation. '*' and '++' are at the same level and belong to right combination, () and -> are at the same level and belong to left combination; that is, from left to right;

#include<stdio.h>
 struct st
{
    int x;
	int *y;
 }*p;
 int dt[4]={10,20,30,40};
 st a[4]={50,&dt[0],60,&dt[1],70,&dt[2],80,&dt[3]
 }; 
int main()
 {
 	p=a;
 	printf("%d,",++p->x);// ->的优先级大于++,所以为先取值,对值进行加加;
 	printf("%d,",(++p)->x);//先执行括号里内容,指针移动,取内容;
 	printf("%d\n",(*p->y)++);//‘->’的优先级高于‘*’,所以先取y,在取y地址的内容;输出以后在对y地址的内容加加也就是20++;
 };
 

Okay, thank you everyone. If there are any mistakes, please correct them immediately. Thank you.

Guess you like

Origin blog.csdn.net/weixin_67118900/article/details/124257742