C language savior (structure elementary--8)

content

1.1 Declaration of Structure

Types of struct members Members of structs can be scalars, arrays, pointers, or even other structs.

1.2 Definition and initialization of structure variables

2.1 Access to structure members

2.2 Structure pointer (Which of the following is better)

When passing parameters to a structure, you need to pass the address of the structure.

3.1 Structure homework exercises


1.1 Declaration of Structure

We know that there are many types in c language: char short int long float double type, but these types cannot represent complex objects

Describe the student: name, age, gender, grades...

Basics of Structures A structure is a collection of values ​​called member variables. Each member of the structure can be a variable of a different type.

//结构体申明:
struct(关键字) tag(名字)
{
 member-list;(成员变量)
}variable-list;(变量列表,可写可不写)

//申明一个学生结构体
struct Stu
{
  char name[20];//名字
  int age;//年龄
  char sex[8];//性别
  float score;//成绩
}s1,s2,s3;           //分号不能丢s1,s2,s3是通过struct Stu类型创建的变量
                      //s1,s2,s3是全局变量



 int main()
{
  struct Stu s4;   //局部变量
}
struct Stu is the structure type, we can't just omit struct, we can use typedef (type redefinition)
typedef struct Stu
{
	char name[20];//名字
	int age;//年龄
	char sex[8];//性别
	float score;
}Stu;//(对整个上面类型重新起名字,不能再创建列表)


 int main()
{
  Stu s6;   //局部变量
}

Types of struct members Members of structs can be scalars, arrays, pointers, or even other structs.

1.2 Definition and initialization of structure variables

struct Point
{
	int x;
	int y;
}p1 = {10, 15};//定义变量p1并初始化

struct S
{
	char c;
	struct Point sp;
	double d;
	char arr[20];
};

int main()
{
	struct Point p = { 100, 200 };//初始化
    struct S ss = { 'w', {100,20}, 5.5, "hello"};
	return 0;
}

2.1 Access to structure members

Members of structure variables are accessed through the structure member operator (.)

struct Point
{
	int x;
	int y;
}p1 = {10, 15};//定义变量p1并初始化

struct S
{
	char c;
	struct Point sp;
	double d;
	char arr[20];
};

int main()
{
	struct Point p = { 100, 200 };//初始化
    struct S ss = { 'w', {100,20}, 5.5, "hello"};
    printf("%c\n", ss.c);//结构体对象.结构体成员 
    printf("%c\n", ss.sp.x,ss.sp.y);
    printf("%c\n", ss.d);
    printf("%c\n", ss.arr);
	return 0;
}

2.2 Structure pointer (Which of the following is better)

struct Point
{
	int x;
	int y;
}p1 = {10, 15};

struct S
{
	char c;
	struct Point sp;
	double d;
	char arr[20];
};

void print1(struct S s)//打印struct S类型的变量,s是一份临时拷贝
{
	printf("%c\n", s.c);
	printf("%d %d\n", s.sp.x, s.sp.y);
	printf("%lf\n", s.d);
	printf("%s\n", s.arr);
}

void print2(struct S* ps)//指针,传过来的是ss地址
{
	printf("%c\n", ps->c);
	printf("%d %d\n", ps->sp.x, ps->sp.y);
	printf("%lf\n", ps->d);
	printf("%s\n", ps->arr);
}

int main()
{
	struct Point p = {100, 200};
	struct S ss = { 'w', {100,20}, 5.5, "hello"};
    //ss.c = 'b';可以改变量
	//ss.sp.x = 1000;
	//ss.sp.y= 2000;
	//ss.d = 3.14;
    //strcpy(ss.arr, "world");//strcpy字符串拷贝
	print1(ss);//打印struct S类型的变量
	print2(&ss);//拿到ss

	return 0;
}

Two copies of variables of type struct S are created, and the parameters need to be pushed onto the stack. If a structure object is passed, the structure is too large, and the system overhead of parameter stacking is relatively large, so the performance will be degraded.

print2 only passes the past ss address, the size is 4/8 bytes, saving space

.struct variables.struct members

-> Structure pointer -> Structure member

When passing parameters to a structure, you need to pass the address of the structure.


3.1 Structure homework exercises

struct student
{
  int num;
  char name[32];
  float score;
}stu;
下面的叙述不正确的是:( )

A.struct 是结构体类型的关键字
B.struct student 是用户定义的结构体类型
C.num, score 都是结构体成员名
D.stu 是用户定义的结构体类型名

A:正确,在C语言中需要自定义类型时,要用到struct关键字

B:正确:在C语言中,用struct定义的结构体,定义结构体类型变量时,需要用struct student

C:正确:结构体中的变量名称,称之为结构体的成员

D:错误:stu是定义的结构体类型变量,不是名称,如果想要让stu为结构体类型名称时,必须在结构体定义时添加   typedef关键字

  

因此:选择D

3.2 The output of the following program is

struct stu
{
    int num;
    char name[10];
    int age;
};


void fun(struct stu *p)
{
	printf(“%s\n”,(*p).name);
	return;
}


int main()
{
	struct stu students[3] = {
   
   {9801,”zhang”,20},
							  {9802,”wang”,19},
                              {9803,”zhao”,18}
                             };
    fun(students + 1);
	return 0;
}

In the main function, an array students of the stu structure type is defined first. Students point to the starting position of the structure. Students+1 indicates the first element in the array. Therefore, the formal parameter of fun actually points to the students array. The first element of , so print is wang

Guess you like

Origin blog.csdn.net/weixin_63543274/article/details/123733170