结构体的指针
#include<stdio.h>struct Date
{
int year;
int month;
int day;
};
struct Book{
char title;
char author;
float price;
struct Date date;
char publisher;
};
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 0;
}
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(void)
{
struct Book b1,b2;
printf("请录入第一本书的信息...\n");
getInput(&b1);
putchar('\n');
printf("请录入第二本书的信息...\n");
getInput(&b2);
printf("\n\n录入完毕,现在开始打印验证...\n\n");
printf("打印第一本书的信息...\n");
printBook(&b1);
putchar('\n');
printf("打印第二本书的信息...\n");
printBook(&b2);
return 0;
}
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 0;
}
这个struct Book*getInput(struct Book *book) 这两个指针分别是什么意思? struct Book*getInput这是定义一个函数
定义函数: 类型 + 名称
说明这是一个结构体指针函数
(struct Book *book)
struct Book 这个是老师自己定义的结构体
struct Book *这代表传入的参数是一个地址是一个形参
后面的名字不重要可以随意更改
页:
[1]