将结构指针数组传递给函数

时间:2016-06-22 11:14:10

标签: c arrays pointers

以下是使用数据结构的图书馆管理系统的代码。 第一个功能是添加一本书。第二是显示所有书籍。我能够输入数据,并从addBookInfo()函数返回数据。但是在发送数据时(在switch语句的情况2中),它的价格为00.000。请帮我纠正错误。

 #include<stdio.h>
    #include<string.h>
    #include<stdbool.h>
    #include<stdlib.h>
    #define MAXCHAR 100



    typedef struct library
    {
            char bookTitle[MAXCHAR];
            char authorName[MAXCHAR];
            float price;
            bool availability;
    }library;


    void addBookInfo(library**book);
    void displayBookInfo(library**);
    //void authorName();
    //void displayTitle();
    //void numberOfBooks();
    int count;


    int main()
    {
            int num,i;
            int select;
            library **book=(library**)malloc(sizeof(library));
     //       library** temp;
    //      library *Head=(library*)malloc(sizeof(library));
    //      *book = Head;

            while(1)
            {
                    printf ("\n1. Add book info.\n 2.Display Book Info\n 3.List all the books of a given author \n 4.List the title of the given book\n");

    //////////////          printf("5. List the count of the books in the library \ņ 7. Exit\n");
                    printf("Select an option\n");
                    scanf("%d",&select);

                    switch (select)
                    {
                            case 1 :addBookInfo(book);
                                    printf("%f...",book[0]->price); //Works price getting displayed
                                   break;

                            case 2 :
    //                              
                                    printf("++++%f++++",book[0]->price); //not working price showing as 00.000
                                    displayBookInfo(book);
                    //              printf("++++%f++++",book[0]->price);
                                    break;
                    /*
                            case 3 :
                                    break;

                            case 4 :
                                    break;

                            case 5 :
                                    break;

                            case 6 :
                                    break;
                    */
                            case 7 :printf(" EXITING...");
                                    exit(0);
                                    break;

                             default : printf(" invalid number \n ");
                    }

            }
            return 0;



    }


    void addBookInfo(library **book )
    {

            library books;
            //library **book=(library**)malloc(sizeof(library)); 
            printf("Enter Book details...\n");
            printf("Enter Book name \n");
            scanf("%s",books.bookTitle);
            printf("Enter author name\n");
            scanf("%s",books.authorName);
            printf("Enter the price \n");
            scanf("%f",&books.price);
            books.availability=true;
            *book=&books;
            *book++;

            count++;
    //      printf("%f",book[0]->price);
    //      return book;
    }

    void displayBookInfo(library**book)
    {
            int i;
            printf("Display info of all the books\n");
            for(i=0;i<count;i++)
            {
                    printf("%f\n,",book[0]->price);// not working price showing as 00.000 or segmentation fault 

            }
    }

1 个答案:

答案 0 :(得分:0)

很多问题。

1)你想在main函数中创建一个库数组,所以你应该至少:

#define BOOK_ARRAY_ELEMS 100
library **book= malloc(sizeof(library*)*BOOK_ARRAY_ELEMS);
if (book == NULL)
{
    exit(1);
}

2)booksaddBookInfo内有本地范围,因此堆栈已分配。您无法导出堆栈分配的var地址。您可以使用malloc来创建这种变量。

3)您使用全局值count来计算插入的书籍,因此您可以使用它来指出数组中正确的元素。

void addBookInfo(library **book )
{
    library *books = malloc(sizeof(library));

    if (books != NULL)
    {
        printf("Enter Book details...\n");
        printf("Enter Book name \n");
        scanf("%s", &books->bookTitle[0]);
        printf("Enter author name\n");
        scanf("%s", &books->authorName[0]);
        printf("Enter the price \n");
        scanf("%f",&books->price);
        books->availability=true;
        book[count]=books;

        count++;
    }
    else
    {
        fprintf(stderr, "Error allocating book\n");
        exit(1);
    }
}

void displayBookInfo(library**book)
{
    int i;
    printf("Display info of all the books\n");
    for(i=0;i<count;i++)
    {
        printf("%f,", book[i]->price);
    }

    printf("\n");
}

不要忘记释放所有malloc个变量,所以案例7

                        case 7 :
                        {
                            printf(" EXITING...");
                            for (i=0; i<count; i++)
                            {
                                free(book[i]);
                            }
                            free(book);
                            exit(0);
                        }
                        break;

顺便说一下,我想你正在努力实现一个连锁列表,这是另一个长篇故事....