鱼C论坛

 找回密码
 立即注册
查看: 720|回复: 5

[已解决]结构体传参 为什么结构体初始化失败呢

[复制链接]
发表于 2024-8-3 21:24:23 | 显示全部楼层 |阅读模式
5鱼币
  1. #include <stdio.h>
  2. struct Date {                 // 申明定义Book结构体的子结构体 Date 变量名为date
  3.                 int year;
  4.                 int month;
  5.                 unsigned int day;
  6. } ;

  7. struct Book {                        //struct申明结构体  Book 结构体名称   
  8.                 char title[128] ;   char author[40] ;  float price ;
  9.                 struct Date date;   char publisher[40];       
  10. } ;

  11. struct Book getinput(struct Book book) ; //申明类型为结构体的函数
  12. struct Book getinput(struct Book book)  // 定义结构体函数
  13. {        
  14.        
  15.                 book.title = "<<带你学C>>";
  16.                 book.author = "小甲鱼";
  17.                 return  book;
  18. }

  19. void printBook(struct Book book);
  20. void printBook(struct Book book)
  21. {
  22.                 printf("书名: %s \n",book.title) ;
  23.                 //printf("出版日期: %d-%d-%d\n",book.date.year,book.date.month,book.date.day);
  24. }
  25. int main(void){
  26.                 struct Book b1;   //
  27.                 b1 = getinput(b1);
  28.                
  29.                 printBook(b1);
  30.        
  31.                 return 0;       
  32.                        
  33. }
复制代码
最佳答案
2024-8-3 21:24:24
主要有以下 3 个问题:

1、不可以直接向字符串变量赋值:
  1.                 book . title = "<<带你学C>>";
  2.                 book . author = "小甲鱼";
复制代码


2、函数的返回值只能是一个数值、指针或布尔值,不可以是结构体。
  1. struct Book getinput(struct Book book)  // 定义结构体函数
  2. {         
  3.         
  4.                 book.title = "<<带你学C>>";
  5.                 book.author = "小甲鱼";
  6.                 return  book;
  7. }
复制代码


3、当一个函数的声明与该函数的定义出现在同一位置时,没有必要进行声明
  1. struct Book getinput(struct Book book) ; //没有必要进行声明
  2. struct Book getinput(struct Book book)  // 定义结构体函数
  3. {         
  4.         
  5.                 book.title = "<<带你学C>>";
  6.                 book.author = "小甲鱼";
  7.                 return  book;
  8. }

  9. void printBook(struct Book book) ; // 没有必要进行声明
  10. void printBook(struct Book book)
  11. {
  12.                 printf("书名: %s \n",book.title) ;
  13.                 //printf("出版日期: %d-%d-%d\n",book.date.year,book.date.month,book.date.day);
  14. }
复制代码

        同时,只有当一个函数的调用位置先于定义位置时,在调用这个函数时才需要对其进行声明,在本例中,函数
  1. struct Book getinput(struct Book book)
  2. {         
  3.         
  4.                 book.title = "<<带你学C>>";
  5.                 book.author = "小甲鱼";
  6.                 return  book;
  7. }
复制代码

        和
  1. void printBook(struct Book book)
  2. {
  3.     printf("书名: %s \n", book.title);
  4.     printf("作者: %s \n", book.author);
  5.     printf("价格: %.2f\n", book.price);
  6.     printf("出版日期: %d-%d-%d\n", book.date.year, book.date.month, book.date.day);
  7. }
复制代码

     的定义位置都在 main() 之前,而这两个函数都是在 main() 中被调用,满足函数定义位置在前,调用位置在后的条件,所以,无需对这两个函数进行声明。
     下面是修改版本,谨供楼主参考
  1. #include <stdio.h>
  2. #include <string.h>

  3. struct Date {
  4.                 int year           ;
  5.                 int month          ;
  6.                 int day            ;
  7. }                                  ;

  8. struct Book {
  9.                 char title[128]    ;
  10.                 char author[40]    ;
  11.                 float price        ;
  12.                 struct Date date   ;
  13.                 char publisher[40] ;        
  14. }                                  ;

  15. void getinput(struct Book * book , const char * title , const char * author , struct Date date) /* 函数只能返回一个数值或指针,无法返回一个结构,函数声明与定义在一起,没有不要进行声明 */
  16. {         
  17.         
  18.                 strcpy(book -> title , title)         ;
  19.                 strcpy(book -> author , author)       ;
  20.                 book -> date . year  = date . year    ;
  21.                 book -> date . month = date . month   ;
  22.                 book -> date . day   = date . day     ;
  23. }

  24. void printBook(struct Book book)
  25. {
  26.                 printf("书名: %s \n" , book . title)                                                          ;
  27.                 printf("出版日期: %d-%d-%d\n" , book . date . year , book . date . month , book . date . day) ;
  28. }

  29. int main(void)
  30. {
  31.                 struct Book b1                                          ;                                                                                ;
  32.                 getinput(& b1 , "<<带你学C>>" , "小甲鱼" , {2024,8,3})  ;                                      ;
  33.                 printBook(b1)                                           ;
  34.                 return 0                                                ;        
  35. }
复制代码

最佳答案

查看完整内容

主要有以下 3 个问题: 1、不可以直接向字符串变量赋值: 2、函数的返回值只能是一个数值、指针或布尔值,不可以是结构体。 3、当一个函数的声明与该函数的定义出现在同一位置时,没有必要进行声明 同时,只有当一个函数的调用位置先于定义位置时,在调用这个函数时才需要对其进行声明,在本例中,函数 和 的定义位置都在 main() 之前,而这两个函数都是在 main() 中被调用,满足函数 ...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-8-3 21:24:24 | 显示全部楼层    本楼为最佳答案   
主要有以下 3 个问题:

1、不可以直接向字符串变量赋值:
  1.                 book . title = "<<带你学C>>";
  2.                 book . author = "小甲鱼";
复制代码


2、函数的返回值只能是一个数值、指针或布尔值,不可以是结构体。
  1. struct Book getinput(struct Book book)  // 定义结构体函数
  2. {         
  3.         
  4.                 book.title = "<<带你学C>>";
  5.                 book.author = "小甲鱼";
  6.                 return  book;
  7. }
复制代码


3、当一个函数的声明与该函数的定义出现在同一位置时,没有必要进行声明
  1. struct Book getinput(struct Book book) ; //没有必要进行声明
  2. struct Book getinput(struct Book book)  // 定义结构体函数
  3. {         
  4.         
  5.                 book.title = "<<带你学C>>";
  6.                 book.author = "小甲鱼";
  7.                 return  book;
  8. }

  9. void printBook(struct Book book) ; // 没有必要进行声明
  10. void printBook(struct Book book)
  11. {
  12.                 printf("书名: %s \n",book.title) ;
  13.                 //printf("出版日期: %d-%d-%d\n",book.date.year,book.date.month,book.date.day);
  14. }
复制代码

        同时,只有当一个函数的调用位置先于定义位置时,在调用这个函数时才需要对其进行声明,在本例中,函数
  1. struct Book getinput(struct Book book)
  2. {         
  3.         
  4.                 book.title = "<<带你学C>>";
  5.                 book.author = "小甲鱼";
  6.                 return  book;
  7. }
复制代码

        和
  1. void printBook(struct Book book)
  2. {
  3.     printf("书名: %s \n", book.title);
  4.     printf("作者: %s \n", book.author);
  5.     printf("价格: %.2f\n", book.price);
  6.     printf("出版日期: %d-%d-%d\n", book.date.year, book.date.month, book.date.day);
  7. }
复制代码

     的定义位置都在 main() 之前,而这两个函数都是在 main() 中被调用,满足函数定义位置在前,调用位置在后的条件,所以,无需对这两个函数进行声明。
     下面是修改版本,谨供楼主参考
  1. #include <stdio.h>
  2. #include <string.h>

  3. struct Date {
  4.                 int year           ;
  5.                 int month          ;
  6.                 int day            ;
  7. }                                  ;

  8. struct Book {
  9.                 char title[128]    ;
  10.                 char author[40]    ;
  11.                 float price        ;
  12.                 struct Date date   ;
  13.                 char publisher[40] ;        
  14. }                                  ;

  15. void getinput(struct Book * book , const char * title , const char * author , struct Date date) /* 函数只能返回一个数值或指针,无法返回一个结构,函数声明与定义在一起,没有不要进行声明 */
  16. {         
  17.         
  18.                 strcpy(book -> title , title)         ;
  19.                 strcpy(book -> author , author)       ;
  20.                 book -> date . year  = date . year    ;
  21.                 book -> date . month = date . month   ;
  22.                 book -> date . day   = date . day     ;
  23. }

  24. void printBook(struct Book book)
  25. {
  26.                 printf("书名: %s \n" , book . title)                                                          ;
  27.                 printf("出版日期: %d-%d-%d\n" , book . date . year , book . date . month , book . date . day) ;
  28. }

  29. int main(void)
  30. {
  31.                 struct Book b1                                          ;                                                                                ;
  32.                 getinput(& b1 , "<<带你学C>>" , "小甲鱼" , {2024,8,3})  ;                                      ;
  33.                 printBook(b1)                                           ;
  34.                 return 0                                                ;        
  35. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-8-3 21:24:32 | 显示全部楼层
看起来你的代码里有几个小问题,让结构体的初始化变得有点“焦头烂额”。让我带你逐个捋一下,看看怎么能让它顺利运行。

### 1. **字符数组的定义问题】
在你的 `Book` 结构体中,`title` 和 `author` 被定义为单个字符(`char`),而你尝试用字符串来初始化它们。C语言中的字符串实际上是一个字符数组。例如,要表示书名和作者,你通常应该使用字符数组:

  1. char title[100];   // 假设书名最多100个字符
  2. char author[50];   // 假设作者名最多50个字符
复制代码


这样才能存放字符串,而不是单个字符。

### 2. **赋值的问题**
你在 `getinput` 函数中尝试用双引号给 `book.title` 赋值,这实际上是错误的。在 C 语言中,字符串需要使用 `strcpy` 函数来复制。例如:

  1. #include <string.h> // 包含这个头文件以使用 strcpy

  2. strcpy(book.title, "书名"); // 用 strcpy 赋值字符串
  3. strcpy(book.author, "小甲鱼");
复制代码


### 3. **未初始化的日期字段**
虽然你注释掉了日期字段,但如果将来需要用到,记得在 `getinput` 中也要为 `date` 的 `year`, `month`, 和 `day` 赋默认值。

### 纠正后的完整代码:
下面是修复后的代码示例:

  1. #include <stdio.h>
  2. #include <string.h> // 引入字符串处理库

  3. struct Date {                 
  4.     int year;
  5.     int month;
  6.     unsigned int day;
  7. };

  8. struct Book {                        
  9.     char title[100];   // 使用字符数组代替单字符
  10.     char author[50];
  11.     float price;
  12.     struct Date date;       
  13.     char publisher[50]; // 同样,出版商也可以用字符数组存储
  14. };

  15. struct Book getinput(struct Book book);

  16. struct Book getinput(struct Book book)  
  17. {        
  18.     strcpy(book.title, "书名");
  19.     strcpy(book.author, "小甲鱼");
  20.     book.price = 39.99; // 示例价格
  21.     book.date.year = 2023; // 示例日期
  22.     book.date.month = 10;
  23.     book.date.day = 5;
  24.     strcpy(book.publisher, "鱼油出版社");
  25.     return book;
  26. }

  27. void printBook(struct Book book);
  28. void printBook(struct Book book)
  29. {
  30.     printf("书名: %s \n", book.title);
  31.     printf("作者: %s \n", book.author);
  32.     printf("价格: %.2f\n", book.price);
  33.     printf("出版日期: %d-%d-%d\n", book.date.year, book.date.month, book.date.day);
  34. }

  35. int main(void) {
  36.     struct Book b1;

  37.     b1 = getinput(b1);
  38.    
  39.     printBook(b1);       
  40.     return 0;       
  41. }
复制代码


这样一来,你的结构体就成功初始并赋值了,再也不会因为初始化失败而焦虑了!如果还有其他问题或者不理解的地方,随时可以问我哦!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

评分

参与人数 1鱼币 +1 收起 理由
阳顺 + 1 鱼C有你更精彩^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-8-3 23:22:23 | 显示全部楼层
jackz007 发表于 2024-8-3 22:32
主要有以下 3 个问题:

1、不可以直接向字符串变量赋值:

第1,3 问题指出的是好,我加以改正
第2个问题,好像小甲鱼课程里说过新的标准支持 结构体函数,2楼改正好了,但结果要想多次使用不理想
你的传参模式很好,只需要在main(),中给多个不一样的参数,可多次使用
要是改为结构体函数,在最后给多个不一样的参数,可多次使用的模式就好了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-8-4 00:48:56 | 显示全部楼层
jackz007 发表于 2024-8-3 22:32
主要有以下 3 个问题:

1、不可以直接向字符串变量赋值:

2、函数的返回值只能是一个数值、指针或布尔值,不可以是结构体。

函数的返回值可以是结构体

  1. sh-5.2$ cat main.c
  2. #include <stdio.h>

  3. struct st {
  4.     int x, y;
  5. };

  6. struct st cxy(int x, int y) {
  7.     return (struct st){x + 1, y + 2};
  8. }

  9. int main(void) {
  10.     struct st xy = cxy(1, 2);
  11.     printf("x: %d, y: %d\n", xy.x, xy.y);
  12.     return 0;
  13. }
  14. sh-5.2$ ./main
  15. x: 2, y: 4
  16. sh-5.2$
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-8-4 07:37:21 | 显示全部楼层
本帖最后由 三体人的智子 于 2024-8-4 07:44 编辑

1问题:在结构体成员赋值时,不能直接使用 book.title = "<<带你学 C>>"和 book.author = "小甲鱼"这样的方式。因为 title 和 author 是字符数组,应该使用字符串复制函数如 strcpy 来进行赋值。


2问题:在 printBook函数中,对于字符数组的输出,也应该使用字符串的输出方式。printf("书名:%s\n",book.title)


  1. #include <stdio.h>
  2. #include <string.h>

  3. struct Date {
  4.     int year;
  5.     int month;
  6.     unsigned int day;
  7. };

  8. struct Book {
  9.     char title[128];
  10.     char author[40];
  11.     float price;
  12.     struct Date date;
  13.     char publisher[40];
  14. };

  15. struct Book getinput(struct Book book) {
  16.     strcpy(book.title, "<<带你学 C>>");
  17.     strcpy(book.author, "小甲鱼");
  18.     return book;
  19. }

  20. void printBook(struct Book book) {
  21.     printf("书名: %s \n", book.title);
  22.   }

  23. int main(void) {
  24.     struct Book b1;
  25.     b1 = getinput(b1);
  26.     printBook(b1);
  27.     return 0;
  28. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 12:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表