简体   繁体   English

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

[英]passing array of structure pointers to a function

Following is the code of a library management system using data structures. 以下是使用数据结构的图书馆管理系统的代码。 the first function is to add a book. 第一个功能是添加一本书。 and the second is to display all the books. 第二个是显示所有书籍。 I am able to input the data , and also return the data from addBookInfo() function. 我可以输入数据,也可以从addBookInfo()函数返回数据。 But when sending the data( in case 2 of switch statement) it is giving price as 00.000. 但是,当发送数据时(在switch语句的情况2中),其价格为00.000。 Please help me to correct the mistake. 请帮助我纠正错误。

 #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 

            }
    }

Lots of problems. 很多问题。

1) You want to create an array of library into main function, so you should have, at least: 1)您要在主函数中创建一个库数组,因此至少应具有:

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

2) books has local scope inside addBookInfo , so is stack allocated. 2) booksaddBookInfo内部有局部作用域,因此是堆栈分配的。 You cannot export stack allocated var address. 您不能导出堆栈分配的变量地址。 You can use malloc to create that kind of variable. 您可以使用malloc创建此类变量。

3) You are using a global value count to count inserted book, so you can use it to point the correct element of your array. 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");
}

Don't forget to free all malloc ated variables, so case 7 不要忘了释放所有malloc ated变量,这样的情况下7

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

BTW I guess you are trying to achieve a concatenated list, that is another long story.... 顺便说一句,我想您正在尝试建立一个串联列表,这又是一个漫长的故事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM