0

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");
    }

}
5
  • ptr = malloc(sizeof(struct Inventory));? ptr is an int* Commented Apr 14, 2015 at 14:04
  • Why do you pass the address of items to display? Just pass item instead of its address . Commented Apr 14, 2015 at 14:06
  • printf("%d\t", items[i][j]); doesn't make any sense at all. You need different format specifiers for each element in Inventory. Commented Apr 14, 2015 at 14:07
  • so like this? printf("%c%.2f%d%d", items.name, items.latestCost,items.stock,items.sold);? Commented Apr 14, 2015 at 14:21
  • 1
    No. Do what @AlterMann suggested in his answer Commented Apr 14, 2015 at 14:22

1 Answer 1

2

Here

void display(struct Inventory *items[][4], int n);

you want a pointer to an array of 4 Inventorys, not a 2d array of pointers to Inventory, change to

void display(struct Inventory items[][4], int n)

or

void display(struct Inventory (*items)[4], int n)

Here

display(&items,n);

you don't need to pass the address

display(items,n);

is enough


printf("%d\t", items[i][j]);

%d prints an integer, use some member of the struct:

printf("%d\t", items[i][j].stock);

or

printf("%d\t", items[i][j].sold);
Sign up to request clarification or add additional context in comments.

6 Comments

Hey, thanks for commenting. I tried your solution but im getting an output of some weird addresses. i'll double check again and ill try to fix it. :)
still getting some bunch of addresses :(
Note that fflush(stdin); is undefined behavior and gets() is dangerous, show us the inputs
i added the fflush(stdin) because whenever I enter how many items in the inventory, after that, it immediately asking for the costs. it skips the name of the item.
The correct way to flush stdin is int c; while ((c = fgetc(stdin)) != '\n' && c != EOF);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.