c - 结构困惑

标签 c function compiler-construction struct scanf

我想创建一个函数,它获取一本书的标题和作者,并通过将它们与给定的结构数组进行比较来返回 0 或 1(如果可用或不可用)...... 编译器显示:

structs.c:10:28: error: expected ‘)’ before ‘title’
structs.c: In function ‘main’:
structs.c:59:21: error: expected expression before ‘bookRecord’
structs.c:60:13: error: expected expression before ‘bookRecord’
structs.c:61:9: warning: implicit declaration of function ‘requestBook’
structs.c:61:23: error: expected expression before ‘bookRecord’

这是代码:

  #include<stdio.h>           
  #include <stdlib.h>
  #include <string.h>


 #define TRUE 1
 #define FALSE 0

 #define NUM_BOOKS 5

 int requestBook(bookRecord title[],bookRecord author[]){    /* compiler error :10*/
          int i;
          for(i=0;i<=NUMBOOKS;i++){
                  if(strcmp(stacks[i].tittle ,bookRecord.title[0]) == 0 &&                                                                              
                  strcmp(stacks[i].author     ,bookRecord.author[0]) == 0 ){

                  return 1;
                   }
           }
                   return 0;
  }

         typedef struct {
        int minute; 
        int hour;   
         } timeT;


    typedef struct {

       char title[50];  
       char author[50];     
       int year;        
       int isOut;                   
       timeT time;      
       int isBlank;         
  } bookRecord;


  /* given array of struct */

    bookRecord stacks[NUM_BOOKS]=
       {
       {"C How To Program", "Deitel", 2006, FALSE, {0,  0}, TRUE} ,
       {"The Old Capital", "Yasunari Kawabata", 1996, FALSE, { 0, 0}, TRUE},
       {"", "", 0, FALSE, {0,0}, FALSE},
       {"", "", 0, FALSE, {0,0}, FALSE},
       {"", "", 0, FALSE, {0,0}, FALSE}
       };

 int main (int argc, char*argv[]) { 
    int t;

    scanf("%s ",bookRecord.title[0]);         /* compiler error :59*/
    scanf("%s",bookRecord.author[0]);     /* compiler error :60*/

     t=requestBook(bookRecord.title[0], bookRecord.author[0]);   /* compiler error :61
     printf("%d",t);


     return 0;
 }

任何帮助表示赞赏!

/////////////////////////////////////////////////////////////////////////////////////////

如果您想查看最终解决方案,请访问 struct confusion(2) 它是重复的

/////////////////////////////////////////////////////////////////////////////////////////

最佳答案

您的代码存在几个问题。

  1. requestBook 函数中使用这些结构后,您可以声明这些结构。
  2. requestBook 函数中,您引用了迄今为止未声明的变量 stacks
  3. requestBook 函数中,您使用 type bookRecord 作为变量。

可能还有更多,但这是我在第一次阅读中找到的。

编辑:

其他一些问题:

  1. requestBook 函数中,您可以循环一次到多次。请记住,数组索引从零到 (number_of_entries - 1)。
  2. 您也可以使用 bookRecord 类型作为 main 中的变量。
  3. 当您错误地使用 bookRecord 时,您不会获得结构或字符串,您只能获得字符串 titleauthor 中的第一个字符>.

关于c - 结构困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12020275/

相关文章:

c - 从 C 中的结构构建字符串

C 程序与共享库链接而不设置 LD_LIBRARY_PATH

c++ - 使用 LLVM 生成纯机器代码

c - 如何避免使用 getchar() 按下 Enter 来仅读取单个字符?

无法使用 I2C 唤醒 Atmel ATSHA204

c - 我需要使用c在另一个文件中打印文件的注释,并从原始文件中删除

multithreading - 如何使 Rust 中的两个函数在生成线程时运行?

MySQL整数作为where子句中的日期

c - C 中的数学函数

haskell - Haskell 中的编译器集成测试