得到错误并帮我解决这个问题

时间:2013-08-15 06:20:40

标签: c object struct resolve

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct books
{
    char book_name[100];
    char book_author[100];
    int book_id;
};
void print(int j,book[j])
{
        printf("The name of book %d is %s",j,book.book_name);
        printf("\nThe author of book %d is %s",j,book.book_author);
        printf("\nThe id of book %d is %d",j,book.book_id);
}
int main()
{
    int b;
    printf("Enter the number of books :");
    scanf("%d",&b);
    for(int i=1;i<=b;i++)
    {
        struct books book[i];
        printf("Enter the details of book %d /n",i);
        printf("Enter the book %d name:",i);
        scanf("%s",&book[i].book_name);
        printf("\n Enter the author of book %d :",i);
        scanf("%s",&book[i].book_author);
        printf("\n Enter the id of book %d :",i);
        scanf("%d",&book[i].book_id);
    }
    printf("\n The details of the books you entered are given below:\n");
    for(int j=1;j<=b;j++)
    {
        print(int j,book[j]);
    }
    getch();
return 0;
}

错误: - &GT; [错误]'book'未在print函数的此范围内声明...如何将struct对象的范围更改为global? 我正在创建一个类似于环境的库,并使用结构打印书籍的名称及其详细信息。但是创建的对象超出了范围。它在错误日志中表示。 帮我解决这个问题。

2 个答案:

答案 0 :(得分:2)

固定代码: - 见评论

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct books
{
    char book_name[100];
    char book_author[100];
    int book_id;
};
void print(int j,struct books book ) //Fix arguments use the struct
{
        printf("The name of book %d is %s",j,book.book_name);
        printf("\nThe author of book %d is %s",j,book.book_author);
        printf("\nThe id of book %d is %d",j,book.book_id);
}
int main()
{
    int b;
    printf("Enter the number of books :");
    scanf("%d",&b);
    struct books book[b];  // Declare the array of struct outside.
    for(int i=1;i<=b;i++)
    {
         // Use \n not /n for newline
        printf("Enter the details of book %d \n",i); 
        printf("Enter the book %d name:",i);

        scanf("%s",book[i].book_name);  // Remove & sign, %s expects a char *
        printf("\n Enter the author of book %d :",i);

        scanf("%s",book[i].book_author); // Remove & sign,  %s expects a char *
        printf("\n Enter the id of book %d :",i);
        scanf("%d",&book[i].book_id);
    }
    printf("\n The details of the books you entered are given below:\n");
    for(int j=1;j<=b;j++)
    {
        print(j,book[j]);
    }
    getch();
return 0;
}

答案 1 :(得分:0)

void print(int j,book[j])

应该是

void print(int j , struct books *books)

和这个

    printf("The name of book %d is %s",j,book.book_name);
    printf("\nThe author of book %d is %s",j,book.book_author);
    printf("\nThe id of book %d is %d",j,book.book_id);

应该是

    printf("The name of book %d is %s",j,books-<book_name);
    printf("\nThe author of book %d is %s",j,books->book_author);
    printf("\nThe id of book %d is %d",j,books->book_id);

在这里你需要改变:

    print(int j,book[j]);

    print(int j, &book[j]);

宣布像这样的函数时

void print(int j,book[j])

您还必须指定缺少的类型。如果您打算直接使用数组而不将其传递给print,那么您必须将其设置为全局,然后您不需要在函数中传递它。尽管如此,Havign it作为参数通常会更好,因为代码可以更好地隔离。