Elementary C Language|Structure

1. The declaration of the structure

A structure is a collection of values ​​that become member variables. Each member of a structure can be a member of a different type A
member of a structure can be a scalar, an array, a pointer, or even another structure.

1.1 Declaration of structure

struct stu
{
    
    
	char name[20];//姓名
	int age;//年龄
	char sex[5];//性别
	char id[20];//学号
};//分号不能丢

Struct is the keyword to define the structure, stu is to define the structure to obtain a name, and it can also be omitted. The curly braces are the members of the structure, and the definition of the structure is a statement that needs to add a semicolon

1.2 Definition and initialization of structure variables

struct stu
{
    
    
	char name[20];//姓名
	int age;//年龄
	char sex[5];//性别
	char id[20];//学号
}S;//声明类型的同时定义变量S
struct stu a;//也可这样用该类型定义变量a
struct stu b={
    
    "张三",18,"男","1314"};//初始化:定义变量的同时赋值

stuct point
{
    
    
    int x;
    int y;
    struct stu c;//结构体嵌套
}p={
    
    4,3{
    
    "丽丽",19,"女","520"}};//结构体嵌套初始化

2. Access to structure members

  • Struct member access

Member access of structure variables is accessed through the dot operator (.). The dot operator takes two operands

struct stu
{
    
    
	char name[20];//姓名
	int age;//年龄
};
struct stu S;

insert image description here
The members of S have name and age; how to access the members?

#include <stdio.h>
#include <string.h>
struct stu
{
    
    
	char name[20];//姓名
	int age;//年龄
};
struct stu S ;
int main()
{
    
    
	
	S.age = 20;//使用.访问age成员
	strcpy(S.name, "张三");
	printf("%d %s", S.age, S.name);
	return 0;
}

As for the assignment of the structure string, it cannot be directly assigned. If we directly use the assignment statement such as S.name = "Li Hua" to assign a value to the member name of a structure variable S, it is actually the address of a string constant The value is assigned to the pointer S.name, and the members of the structure are fundamentally different from the pointer, and cannot be directly assigned. If you want to assign a value to the member name of the structure variable
S, you can use the strcpy function to achieve it.

  • Struct pointer to access member of variable
#include <stdio.h>
struct stu
{
    
    
	char name[20];//姓名
	int age;//年龄
};
void print(struct stu* ps)
{
    
    
	printf("name=%s age=%d\n", (*ps).name, (*ps).age);
	//使用结构体指针访问结构体成员
	printf("name=%s age=%d\n", ps->name, ps->age);
}
int main()
{
    
    
	struct stu s = {
    
     "zhangsan",20 };
	printf(&s);//结构体地址传参
	return 0;
}

There are two access forms for structure pointer access members, one is .operator access members, and the other is -> access members

3. Structure parameter passing

#include <stdio.h>
struct stu
{
    
    
	int data[1000];
	int num;
};
struct stu s = {
    
     {
    
    1,2,3,4},1000 };
//结构体传参,传值
void print1(struct stu s)
{
    
    
	printf("%d\n", s.num);
}
//结构体传参,传址
void print2(struct stu* ps)
{
    
    
	printf("%d\n", ps->num);
}
int main()
{
    
    
	print1(s);//结构体地址传参
	print2(&s);
	return 0;
}

Structure parameter passing supports passing by value and address. When a function passes parameters, it needs to push the stack. When passing values, the formal parameter is a temporary copy of the actual parameter, which will open up a space in the memory, that is, push the stack. If the structure is too large, the system overhead of pushing the stack will be relatively large, which will lead to performance degradation, and the space occupied by the address is very small, so comparing the above two methods of parameter passing, the conclusion of address passing is preferred: structure parameter
passing At this time, the address of the structure should be passed.

Guess you like

Origin blog.csdn.net/weixin_68201503/article/details/130949818