|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
小甲鱼在带你学c里面的一个值传递让我有点疑惑
我把我写的和甲鱼大大的都贴出来。
我是这样写的,也是成功的:
- #include <stdio.h>
- #include <stdlib.h>
- struct Book getInput(void);
- struct Date
- {
- int year;
- int month;
- int day;
- };
- struct Book
- {
- char name[40];
- char title[128];
- float price;
- struct Date date;
- };
- struct Book getInput(void)//《《《《《《《《《《疑惑点《《《《《《《《《《《《《
- {
- struct Book book;
- printf("请输入书名:");
- scanf("%s",book.title);
- printf("请输入姓名:");
- scanf("%s",book.name);
- printf("请输入售价:");
- scanf("%f",&book.price);
- printf("请输入日期:");
- scanf("%d%d%d",&book.date.year,&book.date.month,&book.date.day);
-
- return book;
- }
- void printBook(struct Book book)//将结构体变量 值传递 给指针会使开销变大。
- {
- printf("name:%s\n",book.name);
- printf("title:%s\n",book.title);
- printf("price:%f\n",book.price);
- printf("date:%d%d%d\n",book.date.year,book.date.month,book.date.day);
- }
- int main(void) {
- struct Book b1,b2;
- printf("请输入第一本书的信息》》》\n");
- b1=getInput();
- putchar('\n');
- printf("请输入第二本书的信息》》》\n");
- b2=getInput();
- putchar('\n');
- printf("你的录入:\n");
- printBook(b1);
- printBook(b2);
- return 0;
- }
复制代码
甲鱼老师是这样写的,但我不知道为什么要把b1值传递给函数 ,解释不通啊aaa:
- #include <stdio.h>
- #include <stdlib.h>
- struct Book getInput(struct Book book);
- struct Date
- {
- int year;
- int month;
- int day;
- };
- struct Book
- {
- char name[40];
- char title[128];
- float price;
- struct Date date;
- };
- struct Book getInput(struct Book book)//《《《《《《《《《《《疑惑点《《《《《《《《《
- {
- printf("请输入书名:");
- scanf("%s",book.title);
- printf("请输入姓名:");
- scanf("%s",book.name);
- printf("请输入售价:");
- scanf("%f",&book.price);
- printf("请输入日期:");
- scanf("%d%d%d",&book.date.year,&book.date.month,&book.date.day);
-
- return book;
- }
- void printBook(struct Book book)//将结构体变量 值传递 给指针会使开销变大。
- {
- printf("name:%s\n",book.name);
- printf("title:%s\n",book.title);
- printf("price:%f\n",book.price);
- printf("date:%d%d%d\n",book.date.year,book.date.month,book.date.day);
- }
- int main(void) {
- struct Book b1,b2;
- printf("请输入第一本书的信息》》》\n");
- b1=getInput(b1); //《《《《《《《《《《疑惑点《《《《《《《《《《《《《
- putchar('\n');
- printf("请输入第二本书的信息》》》\n");
- b2=getInput(b2);//《《《《《《《《《《疑惑点《《《《《《《《《《《《《
- putchar('\n');
- printf("你的录入:\n");
- printBook(b1);
- printBook(b2);
- return 0;
- }
复制代码
greenery 发表于 2020-9-27 17:56
我懂了,甲鱼大大的这个代码,把b1值传递给函数局部变量book,同时定义了一个Book结构体类型并且把b1的值传 ...
对啊
|
|