传递结构体指针

#include <stdio.h>



struct Date
{
	int year;
	int month;
	int day;
};

struct Book
{
	char title[128];
	char author[40];
	float price;
	struct Date date;
	char publisher[40];
};


void getinput(struct Book* book);
void printbook(struct Book* book);

void getinput(struct Book* book)
{
	printf("输入书名:\n");
	scanf_s("%s", book->title, 128);
	printf("输入作者:\n");
	scanf_s("%s", book->author, 40);
	printf("输入售价:\n");
	scanf_s("%f", &book->price);
	printf("输入出版日期:\n");
	scanf_s("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
	printf("输入出版社:\n");
	scanf_s("%s", book->publisher, 40);


}

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 Date date = { .year = -1,.month = -1,.day = -1 };

	struct Book b1 = { .author = NULL,.title = NULL,.price = -1,.publisher = NULL };
	struct Book b2 = { .author = NULL,.title = NULL,.price = -1,.publisher = NULL };

	printf("录入第一本书的信息:\n");

	getinput(&b1);

	putchar('\n');

	printf("录入第二本书的信息:\n");
	getinput(&b2);

	putchar('\n');

	printf("\n\n录入完毕,现在开始打印....\n\n");
	printf("打印第一本书的信息...\n");
	printbook(&b1);

	putchar('\n');

	printf("打印第二本书的信息...\n");
	printbook(&b2);

	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值