|
5鱼币
- #include <stdio.h>
- struct Date { // 申明定义Book结构体的子结构体 Date 变量名为date
- int year;
- int month;
- unsigned int day;
- } ;
- struct Book { //struct申明结构体 Book 结构体名称
- char title[128] ; char author[40] ; float price ;
- struct Date date; char publisher[40];
- } ;
- struct Book getinput(struct Book book) ; //申明类型为结构体的函数
- struct Book getinput(struct Book book) // 定义结构体函数
- {
-
- book.title = "<<带你学C>>";
- book.author = "小甲鱼";
- return book;
- }
- void printBook(struct Book book);
- void printBook(struct Book book)
- {
- printf("书名: %s \n",book.title) ;
- //printf("出版日期: %d-%d-%d\n",book.date.year,book.date.month,book.date.day);
- }
- int main(void){
- struct Book b1; //
- b1 = getinput(b1);
-
- printBook(b1);
-
- return 0;
-
- }
复制代码
主要有以下 3 个问题:
1、不可以直接向字符串变量赋值:
- book . title = "<<带你学C>>";
- book . author = "小甲鱼";
复制代码
2、函数的返回值只能是一个数值、指针或布尔值,不可以是结构体。
- struct Book getinput(struct Book book) // 定义结构体函数
- {
-
- book.title = "<<带你学C>>";
- book.author = "小甲鱼";
- return book;
- }
复制代码
3、当一个函数的声明与该函数的定义出现在同一位置时,没有必要进行声明
- struct Book getinput(struct Book book) ; //没有必要进行声明
- struct Book getinput(struct Book book) // 定义结构体函数
- {
-
- book.title = "<<带你学C>>";
- book.author = "小甲鱼";
- return book;
- }
- void printBook(struct Book book) ; // 没有必要进行声明
- void printBook(struct Book book)
- {
- printf("书名: %s \n",book.title) ;
- //printf("出版日期: %d-%d-%d\n",book.date.year,book.date.month,book.date.day);
- }
复制代码
同时,只有当一个函数的调用位置先于定义位置时,在调用这个函数时才需要对其进行声明,在本例中,函数
- struct Book getinput(struct Book book)
- {
-
- book.title = "<<带你学C>>";
- book.author = "小甲鱼";
- 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);
- }
复制代码
的定义位置都在 main() 之前,而这两个函数都是在 main() 中被调用,满足函数定义位置在前,调用位置在后的条件,所以,无需对这两个函数进行声明。
下面是修改版本,谨供楼主参考
- #include <stdio.h>
- #include <string.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 , const char * title , const char * author , struct Date date) /* 函数只能返回一个数值或指针,无法返回一个结构,函数声明与定义在一起,没有不要进行声明 */
- {
-
- strcpy(book -> title , title) ;
- strcpy(book -> author , author) ;
- book -> date . year = date . year ;
- book -> date . month = date . month ;
- book -> date . day = date . day ;
- }
- void printBook(struct Book book)
- {
- printf("书名: %s \n" , book . title) ;
- printf("出版日期: %d-%d-%d\n" , book . date . year , book . date . month , book . date . day) ;
- }
- int main(void)
- {
- struct Book b1 ; ;
- getinput(& b1 , "<<带你学C>>" , "小甲鱼" , {2024,8,3}) ; ;
- printBook(b1) ;
- return 0 ;
- }
复制代码
|
最佳答案
查看完整内容
主要有以下 3 个问题:
1、不可以直接向字符串变量赋值:
2、函数的返回值只能是一个数值、指针或布尔值,不可以是结构体。
3、当一个函数的声明与该函数的定义出现在同一位置时,没有必要进行声明
同时,只有当一个函数的调用位置先于定义位置时,在调用这个函数时才需要对其进行声明,在本例中,函数
和
的定义位置都在 main() 之前,而这两个函数都是在 main() 中被调用,满足函数 ...
|