Making the member of a structure of type structure double pointer

Viewed 57

Suppose I have a structure named 'Item'. I have to make a member of Item double pointer inside another structre.

struct Item{
    char *name;
    int price;
    double weight;
};

struct Inventory{
    struct Item double* item;
    int NUMITEMS;
};
1 Answers

Unfortunately, the term double pointer is ambiguous. It can mean two different things:

  1. Pointer to double double *ptr;

  2. Pointer to pointer T **ptr; where T is any type. It could be double :)

Which of these is depending on context.

So in your case, do this:

struct Inventory{
    struct Item **item;
    int NUMITEMS;
};

Accessing these is not that tricky. You could do something like this:

struct Inventory inventory;
// Do something
struct Item *item = inventory.item[index];
struct Item item2 = *(inventory.item[index]);
Related