gpt4 book ai didi

c - 将结构体指针数组传递给函数

转载 作者:行者123 更新时间:2023-11-30 16:59:50 26 4
gpt4 key购买 nike

以下是使用数据结构的图书馆管理系统的代码。第一个功能是添加书籍。二是展示所有书籍。我可以输入数据,也可以从 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)你想在主函数中创建一个库数组,所以你至少应该有:

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

顺便说一句,我猜你正在尝试实现一个串联列表,这是另一个很长的故事......

关于c - 将结构体指针数组传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37966396/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com