在C语言中,可以使用以下方法来调用结构体:
通过结构体变量名直接访问结构体成员:
struct Student {
int id;
char name[20];
int age;
};
struct Student stu;
stu.id = 1;
strcpy(stu.name, "John");
stu.age = 20;
通过指针访问结构体成员:
struct Student {
int id;
char name[20];
int age;
};
struct Student stu;
struct Student *pStu = &stu;
pStu->id = 1;
strcpy(pStu->name, "John");
pStu->age = 20;
通过函数参数传递结构体:
struct Student {
int id;
char name[20];
int age;
};
void setStudent(struct Student *stu) {
stu->id = 1;
strcpy(stu->name, "John");
stu->age = 20;
}
struct Student stu;
setStudent(&stu);
上述三种方法都可以用来调用结构体,并对结构体进行赋值或访问成员。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: c语言如何新建源文件