结构混乱 [英] struct confusion
问题描述
我想打一个函数,它接受的标题和一书的作者,并返回0或1,如果它是可用的,通过将它们与结构的给定阵列比较.....
编译器显示:
I want to make a function which takes the title and the author of a book and returns 0 or 1 if it is available or not, by comparing them with a given array of structs..... Compiler shows:
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’
下面是code:
#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;
}
任何帮助AP preciated!
Any help appreciated !
/////////////////////////////////////////////// /
////////////////////////////////////////////////
如果你想看到最终的解决方案,请访问STRUCT惑(2)是DUPLICATE
IF YOU WANT TO SEE THE FINAL SOLUTION PLEASE VISIT STRUCT CONFUSION(2) IT IS DUPLICATE
/////////////////////////////////////////////// /
////////////////////////////////////////////////
推荐答案
有几个问题你code。
There are several problems with you code.
- 您声明结构的之后的你在
requestBook
函数中使用它们。 - 在
requestBook
函数,你参考,到目前为止,未声明的变量堆栈
。 - 在您使用
requestBook
函数的键入的bookRecord
作为一个变量。
- You declare the structures after you use them in the
requestBook
function. - In the
requestBook
function you reference the, so far, undeclared variablestacks
. - In the
requestBook
function you use the typebookRecord
as a variable.
有可能更多,但是这是我能在我的一读发现。
There are probably more, but that was what I could find in my first reading.
编辑:
一对夫妇的其他问题:
- 在
requestBook
函数你循环的一次性许多的。请记住,数组索引是从0到(number_of_entries - 1)。 - 您使用类型
bookRecord
作为一个变量主
以及 - 当你错误地使用
bookRecord
你没有得到一个结构或字符串,你得到的第一个字符的字符串标题
和作者
。
- In the
requestBook
function you loop one time to many. Remember that array indexes are from zero to (number_of_entries - 1). - You use the type
bookRecord
as a variable inmain
as well. - When you erroneously use
bookRecord
you do not get a structure or a string, you get only the first character in the stringstitle
andauthor
.
这篇关于结构混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!