结构体的定义
语法
struct 标签名
{
类型名1 成员变量1;
类型名2 成员变量2;
类型名3 成员变量3;
......
}结构体变量列表名;
- 结构体类型名指的是
struct 标签名
- 在结构体变量列表中,多个变量之间用
,
分隔
方式一
有标签名,有变量名
struct Book
{
int id;
char title[50];
char author[50];
char subject[100];
}book; //使用book={100,"C语言","谭浩强","C语言基础知识"};可以在定义的同时进行结构体变量初始化
- 定义好结构体的同时,声明了结构体变量,需要结构体变量时可以再声明
方式二
无标签名
struct
{
int id;
char title[50];
char author[50];
char subject[100];
}book={100,"C语言","谭浩强","C语言基础知识"};//因为无法再声明变量,所以定义结构体时就要声明变量
- 结构体变量仅限于已定义的,不可以再声明其他的结构体变量
方式三
无变量名
struct Book
{
int id;
char title[50];
char author[50];
char subject[100];
};
- 需要结构体变量时再声明
typedef为结构体类型定义别名
有标签,有别名
typedef struct Book
{
int id;
char title[50];
char author[50];
char subject[100];
}Book;//这里的Book相当于struct Book,而不是变量名。
//使用方法
Book book = {100,"C语言","谭浩强","C语言基础知识"};
- 可以使用
Book 变量名
来声明结构体变量,缺点是不可以在定义时声明结构体变量
无标签
typedef struct
{
int id;
char title[50];
char author[50];
char subject[100];
}Book;//这里的Book相当于struct,而不是变量名。
//使用方法
Book book = {100,"C语言","谭浩强","C语言基础知识"};
- 可以使用
Book 变量名
来声明结构体变量,缺点是不可以在定义时声明结构体变量
无别名
typedef struct Book
{
int id;
char title[50];
char author[50];
char subject[100];
};
//使用方法
struct Book book = { 100,"C语言","谭浩强","C语言基础知识" };
结构体变量
定义与初始化
定义结构体时声明结构体变量
struct Book
{
int id;
char title[50];
char author[50];
char subject[100];
}book = {100,"C语言","谭浩强","C语言基础知识"};
定义结构体后声明结构体变量
#include "stdio.h"
struct Book
{
int id;
char title[50];
char author[50];
char subject[100];
};
int main(){
//c中的声明方式
struct Book book = {100,"C语言","谭浩强","C语言基础知识"};
//c++中的支持的声明方式
//struct Book book = {100,"C语言","谭浩强","C语言基础知识"};
//Book book = {100,"C语言","谭浩强","C语言基础知识"};
printf("ID号:%d\n书名:%s\n作者:%s\n主题:%s\n",book.id,book.title,book.author,book.subject);//引用结构体变量中的成员变量
}
输入与输出
#include "stdio.h"
struct Book
{
int id;
char star;
char title[50];
char author[50];
char subject[100];
};
int main(){
struct Book book;
scanf("%d %c %s %s %s",&book.id,&book.star,book.title,book.author,book.subject);
printf("ID号:%d\n星级:%c\n书名:%s\n作者:%s\n主题:%s\n",book.id,book.star,book.title,book.author,book.subject);
}
结构体数组
定义和初始化
定义时初始化数组
struct Student
{
unsigned num;
char name[20];
char sex;
int age;
float score;
char address[50];
}stu[3]={
{950201,"Zhangjun",'M',20,85.0,"Beijing"},
{950202,"Wanghai",'M',19,70.0,"Shanghai"},
{950203,"wangwei",'F',20,88.0,"Wuhan"}
};
定义后再初始化数组
#include "stdio.h"
struct Student
{
unsigned num;
char name[20];
char sex;
int age;
float score;
char address[50];
}stu[3];
int main(){
struct Student stu[3]={
{950201,"Zhangjun",'M',20,85.0,"Beijing"},
{950202,"Wanghai",'M',19,70.0,"Shanghai"},
{950203,"wangwei",'F',20,88.0,"Wuhan"}
};
}
输入和输出
#include "stdio.h"
struct Student
{
unsigned num;
char name[20];
char sex;
int age;
float score;
char address[50];
};
int main(){
struct Student stu[3]={
{950201,"Zhangjun",'M',20,85.0,"Beijing"},
{950202,"Wanghai",'M',19,70.0,"Shanghai"},
{950203,"wangwei",'F',20,88.0,"Wuhan"}
};
for(int i=0;i<3;i++){
scanf("%d %s %c %d %f %s",&stu[i].num,stu[i].name,&stu[i].sex,&stu[i].age,&stu[i].score,stu[i].address);
}
for(int j=0;j<3;j++){
printf("%d %s %c %d %f %s\n",stu[j].num,stu[j].name,stu[j].sex,stu[j].age,stu[j].score,stu[j].address);
}
}
结构体操作
结构体变量之间的赋值
#include "stdio.h"
struct Student
{
unsigned num;
char name[20];
char sex;
int age;
float score;
char address[50];
};
int main(){
struct Student stu[3]={
{950201,"Zhangjun",'M',20,85.0,"Beijing"},
{950202,"Wanghai",'M',19,70.0,"Shanghai"},
{950203,"wangwei",'F',20,88.0,"Wuhan"}
};
struct Student student1,student2,student3,students;
student1 = stu[0];//支持同一结构体类型变量之间的赋值
student2 = stu[1];
student3 = stu[2];
//students = stu;//不支持同一结构体类型数组之间的直接赋值
printf("%d %s %c %d %f %s\n",student1.num,student1.name,student1.sex,student1.age,student1.score,student1.address);
printf("%d %s %c %d %f %s\n",student2.num,student2.name,student2.sex,student2.age,student2.score,student2.address);
printf("%d %s %c %d %f %s\n",student3.num,student3.name,student3.sex,student3.age,student3.score,student3.address);
}
多维结构体
#include "stdio.h"
struct Date
{
int year;
int month;
int day;
};
struct Student
{
unsigned num;
char name[20];
char sex;
struct Date birthday;
};
int main(){
struct Student stu = {1000,"Zhangjun",'M',{1999,10,9}};
printf("%d %s %c %d %d %d\n",stu.num,stu.name,stu.sex,stu.birthday.year,stu.birthday.month,stu.birthday.day);
}