-1
#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;
}

错误:--> [错误] 'book' 没有在打印函数的这个范围内声明......我怎样才能将结构对象的范围更改为全局?我正在创建一个类似环境的库,并使用结构打印书籍的名称及其详细信息。但是创建的对象超出了范围。它在错误日志中说。帮我解决prblm。

4

2 回答 2

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;
}
于 2013-08-15T06:29:12.573 回答
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])

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

于 2013-08-15T06:28:04.597 回答