我想创建一个函数,它获取一本书的标题和作者,并通过将它们与给定的结构数组进行比较来返回 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) 它是重复的
/////////////////////////////////////////////////////////////////////////////////////////
最佳答案
您的代码存在几个问题。
- 在
requestBook
函数中使用这些结构后,您可以声明这些结构。 - 在
requestBook
函数中,您引用了迄今为止未声明的变量stacks
。 - 在
requestBook
函数中,您使用 typebookRecord
作为变量。
可能还有更多,但这是我在第一次阅读中找到的。
编辑:
其他一些问题:
- 在
requestBook
函数中,您可以循环一次到多次。请记住,数组索引从零到 (number_of_entries - 1)。 - 您也可以使用
bookRecord
类型作为main
中的变量。 - 当您错误地使用
bookRecord
时,您不会获得结构或字符串,您只能获得字符串title
和author
中的第一个字符>.
关于c - 结构困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12020275/