目录
一、结构体嵌套
二、结构体数组
三、结构体指针
四、传递结构体变量
五、传递结构体指针
六、动态申请结构体
下面再Book结构体中嵌套调用结构体Date
#include
struct Date{
int year;
int month;
int day;
};
struct Book{
char title[128];
char author[40];
float price;
struct Date date;
char publisher[40];
}book={"资本论","马克思",200.1f,{2019,6,9},"人民邮电出版社"};
int main(){
printf("书名:%s\n",book.title);
printf("作者:%s\n",book.author);
printf("价格:%f\n",book.price);
printf("日期:%d-%d-%d\n",book.date.year,book.date.month,book.date.day);
printf("出版社:%s\n",book.publisher);
return 0;
}
方法1:
struct 结构体名称{
结构体成员;
}数组名[长度];
方法2:先声明一个结构体类型,再用此类型定义一个结构体数组
struct 结构体名称{
结构体成员;
};
struct 结构体名称 数组名[长度];
#include
struct Date{
int year;
int month;
int day;
};
struct Book{
char title[128];
char author[40];
float price;
struct Date date;
char publisher[40];
}book;
int main(){
struct Book book[3]={
{"资本论","马克思",200.1f,{2019,6,9},"人民邮电出版社"},
{"数学","高斯",200.1f,{2019,6,9},"清华大学出版社"},
{"历史","司马迁",200.1f,{2019,6,9},"机械工业出版社"}
};
printf("书名:%s\n",book[1].title);
printf("作者:%s\n",book[1].author);
printf("价格:%f\n",book[1].price);
printf("日期:%d-%d-%d\n",book[1].date.year,book[1].date.month,book[1].date.day);
printf("出版社:%s\n",book[1].publisher);
return 0;
}
可细分为指向结构体变量的指针和指向结构体数组的指针
通过结构体指针访问结构体成员又两种方法:
(*结构体指针).成员名
#include
struct Date{
int year;
int month;
int day;
};
struct Book{
char title[128];
char author[40];
float price;
struct Date date;
char publisher[40];
}book={"历史","司马迁",200.1f,{2019,6,9},"机械工业出版社"};
int main(){
struct Book *pt;
//*pt表示提取pt指针指向的地址保存的值
pt=&book; //把book的地址,赋值给指针变量pt ,此时 pt中保存了地址
printf("书名:%s\n",(*pt).title);
printf("作者:%s\n",(*pt).author);
printf("价格:%f\n",(*pt).price);
printf("日期:%d-%d-%d\n",(*pt).date.year,(*pt).date.month,(*pt).date.day);
printf("出版社:%s\n",(*pt).publisher);
return 0;
}
结构体指针->成员名
#include
struct Date{
int year;
int month;
int day;
};
struct Book{
char title[128];
char author[40];
float price;
struct Date date;
char publisher[40];
}book={"历史","司马迁",200.1f,{2019,6,9},"机械工业出版社"};
int main(){
struct Book *pt;
//*pt表示提取pt指针指向的地址保存的值
pt=&book; //把book的地址,赋值给指针变量pt ,此时 pt中保存了地址
printf("书名:%s\n",pt->title);
printf("作者:%s\n",pt->author);
printf("价格:%f\n",pt->price);
printf("日期:%d-%d-%d\n",pt->date.year,pt->date.month,pt->date.day);
printf("出版社:%s\n",pt->publisher);
return 0;
}
可以看到pt-> 和(*pt). 的作用是一样的。
#include
int main(){
struct Test{
int x;
int y;
}t1,t2;
t1.x=3;
t1.y=4;
t2=t1;
printf("t2.x=%d t2.y=%d",t2.x,t2.y);
return 0;
}
在结构变量赋值时必须类型相同
#include
struct Date{
int year;
int month;
int day;
};
struct Book{
char title[100];
char author[100];
float price;
struct Date date;
char publisher[100];
};
struct Book getInput(struct Book book);
void printBook(struct Book book);
struct Book getInput(struct Book book){
printf("请输入书名:");
scanf("%s",book.title);
printf("请输入作者:");
scanf("%s",book.author);
printf("请输入售价:");
scanf("%f",&book.price);
printf("请输入日期:");
scanf("%d-%d-%d",&book.date.year,&book.date.month,&book.date.day);
printf("请输入出版社:");
scanf("%s",book.publisher);
return book;
}
void printBook(struct Book book){
printf("书名:%s\n",book.title);
printf("作者:%s\n",book.author);
printf("售价:%.2f\n",book.price);
printf("日期:%d -%d- %d\n",book.date.year,book.date.month,book.date.day);
printf("出版社:%s\n",book.publisher);
}
int main(){
struct Book b1,b2;
printf("请输入第一本书的信息\n");
b1=getInput(b1);
putchar('\n');
printf("请输入第二 本书的信息\n");
b2=getInput(b2);
printf("\n\n录入完毕,开始打印验证......\n");
printf("打印第一本书的信息:\n");
printBook(b1);
printf("\n");
printf("打印第二本书的信息:\n");
printBook(b2);
return 0;
}
#include
struct Date{
int year;
int month;
int day;
};
struct Book{
char title[100];
char author[100];
float price;
struct Date date;
char publisher[100];
};
void getInput(struct Book *book){
printf("请输入书名:");
scanf("%s",book->title);
printf("请输入作者:");
scanf("%s",book->author);
printf("请输入售价:");
scanf("%f",&book->price);
printf("请输入日期:");
scanf("%d-%d-%d",&book->date.year,&book->date.month,&book->date.day);
printf("请输入出版社:");
scanf("%s",book->publisher);
}
void printBook(struct Book *book){
printf("书名:%s\n",book->title);
printf("作者:%s\n",book->author);
printf("售价:%.2f\n",book->price);
printf("日期:%d -%d- %d\n",book->date.year,book->date.month,book->date.day);
printf("出版社:%s\n",book->publisher);
}
int main(){
struct Book b1,b2;
printf("请输入第一本书的信息\n");
getInput(&b1);
putchar('\n');
printf("请输入第二 本书的信息\n");
getInput(&b2);
printf("\n\n录入完毕,开始打印验证......\n");
printf("打印第一本书的信息:\n");
printBook(&b1);
printf("\n");
printf("打印第二本书的信息:\n");
printBook(&b2);
return 0;
}
使用malloc为结构体分配存储空间
#include
#include
struct Date{
int year;
int month;
int day;
};
struct Book{
char title[100];
char author[100];
float price;
struct Date date;
char publisher[100];
};
void getInput(struct Book *book){
printf("请输入书名:");
scanf("%s",book->title);
printf("请输入作者:");
scanf("%s",book->author);
printf("请输入售价:");
scanf("%f",&book->price);
printf("请输入日期:");
scanf("%d-%d-%d",&book->date.year,&book->date.month,&book->date.day);
printf("请输入出版社:");
scanf("%s",book->publisher);
}
void printBook(struct Book *book){
printf("书名:%s\n",book->title);
printf("作者:%s\n",book->author);
printf("售价:%.2f\n",book->price);
printf("日期:%d -%d- %d\n",book->date.year,book->date.month,book->date.day);
printf("出版社:%s\n",book->publisher);
}
int main(){
struct Book *b1,*b2;
b1=(struct Book *)malloc(sizeof(struct Book));
b2=(struct Book *)malloc(sizeof(struct Book));
if(b1==NULL||b2==NULL){
printf("分配内存失败");
exit(1);
}
printf("请输入第一本书的信息\n");
getInput(b1);
putchar('\n');
printf("请输入第二 本书的信息\n");
getInput(b2);
printf("\n\n录入完毕,开始打印验证......\n");
printf("打印第一本书的信息:\n");
printBook(b1);
printf("\n");
printf("打印第二本书的信息:\n");
printBook(b2);
free(b1);
free(b2);
return 0;
}