Accessing the member of a structure by double pointer of that structure type

Viewed 107

I have a structure Item with the following members.

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

Another structure Inventory with the following members.

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

I have a function addItem() which takes the Inventory* as its parameter.

void addItem(struct Inventory* i){
     printf("Enter Name:");
     scanf("%s",i->item->name);
     printf("Enter Price");
     scanf("%d",i->item->price);
     printf("Enter Weight");
     scanf("%lf",i->item->weight);
}

The main objective of the function is to assign the values to the member of Item structure. How can I do so? I want to set the values of the members of the Item structure through the double pointer of type item from a function. My entire code is:

#include<stdio.h>
 struct Item{
  char *name;
  int price;
  double weight;
   };

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

struct Character{
 char *name;
 int Level;
 long XP;
 struct Inventory inventory;
 };
  void createCharacter(struct Character* c){
    printf("Enter Name:");
    c->name = (char*) malloc(40*sizeof(char));
    scanf("%s",c->name);
    printf("Enter Level:");
    scanf("%d",&c->Level);
    printf("Enter XP:");
   scanf("%ld",&c->XP);
  }
void viewCharacter(struct Character* b){
printf("%s level %d with %ld XP",b->name,b->Level,b->XP);
  }
 void addItem(struct Inventory* i){
 i->item =(struct Item*)malloc(i->NUMITEMS*sizeof(struct Item));
 printf("Enter name:");
 scanf("%s",i->item->(*name));
 }

void viewItem(struct Inventory* i){
   //yet to write
 }
1 Answers

This structure ...

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

... has the appearance of a wrapper for an array, perhaps dynamically allocated, of pointers to struct Item. Instances' item members point to the first pointer in that array. There is more than one way that the members of the items pointed to by the array elements can be accessed, but since the structure represents an array, the most natural and intuitive way would probably be to use array syntax:

struct Inventory *inv = /* ... */ ;

for (int i = 0; i < inv->NUMITEMS; ++i) {
    printf("%s\n",
            // here:
            inv->item[i]->name
    );
}

Explanation: with inv being a pointer to a struct Inventory, the indirect member-access operator is natural for accessing its members, such as inv->item. For its part, inv->item is a pointer to another pointer. You have to dereference it and then dereference the result to get to a struct Item. Although you could use the dereferencing operator (unary *) directly for that purpose, the array indexing operator [] and the indirect member access operator -> both dereference as part of their behavior (always), so you get the needed two derferencings by indexing into the array of items and using the indirect access operator to get the member of interest.

Additional notes:

  • the name of the item member of struct Inventory may be misleading you, both in that it is a pointer to a pointer to an item rather than a pointer directly to an item, and in that it has every appearance of serving as a pointer to the first of (possibly) several item pointers in an array of such, despite its name being singular.

  • the signature of your viewItem() function and maybe also of your addItem() function therefore seems to be insufficient for the apparent task. If you want to view a particular item then you need to specify which one, presumably by passing its index as an additional argument. The same if you want to add an item at a specific position (as opposed to appending after the last item already present).

Related