Problems with passing an array of structures by pointer
Hello everyone.
I have looked through other posts and have not found a solution to my problem but if there is another thread with the same problem please reply with the URL.
This is a first time post for me. I am having trouble with the function Additem which should take a pointer to an array called 'inventory', of structures called 'vbook's then modify the the structure in a certain coordinate within the array. I cannot tell what I am doing wrong but I think it has something to do with the way that I am passing the array or in the prototype and definition of the structure.
I have eliminated a lot of code that has nothing to do with my problem (I think). Any help would be appreciated as this has been driving me crazy for the last 3 days.
Thank you.
Let me know if there is any other information you need.
Code:
#define INV_SIZE 20
#define SIZE 30
#define INITIAL_SIZE 5
#include <stdio.h>
#include <string.h>
typedef struct
{
char book_name[SIZE];
char publisher[SIZE];
int in_stock;
int isbn;
double cost;
double srp;
}vbook;
void Initialize(vbook *inventory[], int *inv_size);
int Menu();
void AddNewItem(vbook *inventory[], int *inv_size);
int main()
{
vbook *inventory[INV_SIZE];
int selection = 0;
int *inv_size;
int exit = 0;
*inv_size = 5;
FILE *fp;
fp = fopen("report.txt", "w");
do{
selection = Menu();
switch (selection)
{
case 1:
AddNewItem(inventory, inv_size);
break;
}
}while(exit != -1);
return(0);
}
int Menu()
{
int selection;
printf("Enter 1 to add a new item to inventory.\n");
printf("Enter 2 to delete an item from inventory.\n");
printf("Enter 3 to display all inventory records.\n");
printf("Enter 4 to create a current inventory report.\n");
printf("Enter 5 to clear entire inventory.\n");
printf("Enter 6 to Exit.\n");
scanf("%d", &selection);
return(selection);
}
void AddNewItem(vbook *inventory[], int *inv_size)
{
int i = 0;
int exit = 0;
do{
if(i > INV_SIZE)
{
printf("Your inventory size has surpassed available memory.\n");
printf("Please delete some items from inventory or increase inventory size.\n");
exit = -1;
}
if(inventory[i].isbn == -1 && exit == 0)
{
printf("Please enter the name of the book.\n");
scanf("%s", inventory[i]->book_name);
printf("Please enter the publisher of the book.\n");
scanf("%s", inventory[i]->publisher);
printf("Please enter the number of books in stock.\n");
scanf("%d", &inventory[i]->in_stock);
printf("Please enter the ISBN number of the book.\n");
scanf("%d", &inventory[i]->isbn);
printf("Please enter the suppliers cost for one book.\n");
scanf("%lf", &inventory[i]->cost);
printf("Please enter the retail price for the book.\n");
scanf("%lf", &inventory[i]->srp);
exit = -1;
*inv_size = (*inv_size + 1);
}
else
i++;
}while(exit == 0 && i < INV_SIZE);
}