I'm new to 2D arrays, structs and have a limited knowledge about pointers. My problem is that the display function only displays addresses. i dont know which part of my code is wrong or missing. can you give me suggestions to fix it or ideas? Thanks!
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <assert.h>
struct Inventory
{
char name[100];
float latestCost;
int stock;
int sold;
};
struct Inventory *getInfo(void);
void display(struct Inventory *items[][4], int n);
main()
{
char select;
struct Inventory items[10][4];
int i,n,j, *ptr;
printf("\nEnter how many items in the inventory:\n");
scanf("%d", &n);
ptr = malloc(sizeof(struct Inventory));
for(i= 0; i < n; i++)
{
items[i][4] = *getInfo();
}
display(&items,n);
getch();
}
struct Inventory *getInfo(void)
{
struct Inventory *items = malloc(sizeof(struct Inventory));
assert(items != NULL);
fflush(stdin);
printf("\nName of the item: \n");
gets(items->name);
printf("\nCost:");
scanf("%f", &items->latestCost);
printf("\nStock:");
scanf("%d", &items->stock);
printf("\nTotal Sold:\n");
scanf("%d", &items->sold);
return items;
}
void display(struct Inventory *items[][4], int n)
{
int i, j;
for(i = 0; i < n; i++)
{
for(j = 0; j < 4; j++)
{
printf("%d\t", items[i][j]);
}
printf("\n");
}
}
ptr = malloc(sizeof(struct Inventory));?ptris anint*itemsto display? Just passiteminstead of its address .printf("%d\t", items[i][j]);doesn't make any sense at all. You need different format specifiers for each element inInventory.