1

I am trying to create an array of pointers that points to another array of structs. However, I am confused as to whether I should declare the new array as an int or the same type as the first array since it is going to hold pointers. This is what I have so far:

struct inventoryItem
{
    int itemNumber;
    int itemsInStock;
    float cost;
    float retailPrice;

};


int main()
{
    printf("Enter the number of slots needed in the array: ");
    int size;
    scanf("%d", &size);

    //array of items
    struct inventoryItem *inventory; //use pointer to item 
    inventory =(struct inventoryItem *) malloc(sizeof(struct inventoryItem)*size); //create array to store inventoryItem with size 'size'

    //array of index
    //int *indexArray = (int*) malloc(sizeof(int)*size); 
    struct inventoryItem *indexArray; //use pointer to item 
    indexArray =(struct inventoryItem *) malloc(sizeof(struct inventoryItem)*size); //create array to store inventoryItem with size 'size'

    //fill array contents
    for(int i = 0; i < size; i++)
    {
        printf("Enter item %d number: ", i);
        scanf("%d", &inventory[i].itemNumber);

        printf("Enter item %d stock: ", i);
        scanf("%d", &inventory[i].itemsInStock);

        printf("Enter item %d cost: ", i);
        scanf("%f", &inventory[i].cost);

        printf("Enter item %d price: ", i);
        scanf("%f", &inventory[i].retailPrice);
    }

    for(int i = 0; i < size; i++)
    {
        printf("Item %d number: %d\n", i, inventory[i].itemNumber);
        printf("Item %d stock: %d\n", i, inventory[i].itemsInStock);
        printf("Item %d cost: %f\n", i, inventory[i].cost);
        printf("Item %d retail price: %f\n", i, inventory[i].retailPrice);
    }


    //struct inventoryItem *header = inventory;
    //struct inventoryItem *ptr = inventory;
    for(int i = 0; i < size; i++)
    {
        indexArray[i] = &inventory[i]; 
            //problem here, it says "no operator '=' matches these operands"

    }
}

EDIT: Now that I created the array, how can I print the content of inventory using indexArray?

user3188716
  • 29
  • 1
  • 1
  • 4
  • 1
    Unrelated: [Stop casting `malloc()` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – WhozCraig Mar 31 '14 at 05:05
  • You're not creating an array of pointers, which would be the first problem. You're creating to arrays of `inventoryItem`. – WhozCraig Mar 31 '14 at 05:07

5 Answers5

2

You should allocate array of pointers:

struct inventoryItem **indexArray;
indexArray = (struct inventoryItem **)malloc(sizeof(struct inventoryItem*)*size);
curandero
  • 141
  • 3
1

Declare `indexArray' as

struct inventoryItem **indexArray;

create it with

indexArray =(struct inventoryItem **) malloc(sizeof(struct inventoryItem *)*size);

and fill with

indexArray[i] = &inventory[i]; 

or

indexArray[i] = inventory + i; 
CiaPan
  • 9,381
  • 2
  • 21
  • 35
  • how can I print the inventory items' contents using the indexArray? – user3188716 Mar 31 '14 at 05:11
  • Every `indexArray` item is a pointer to an `inventory` item so just use `indexArray[i]->itemsInStock` (or longer, but equivalent, `(*indexArray[i]).itemsInStock`) – CiaPan Mar 31 '14 at 05:13
1

for readability it is better to do a typedef first:

typedef struct inventoryItem Inv;

If you want to create an array of pointers to the struct array then instead of

Inv* indexArray = malloc(sizeof(Inv)*size); 

write

Inv** indexArray = malloc(sizeof(Inv*)*size);
AndersK
  • 35,813
  • 6
  • 60
  • 86
0
struct inventoryItem **inventory; //use pointer to item 
inventory =(struct inventoryItem **) malloc(sizeof(struct inventoryItem*) * size);
BlackMamba
  • 10,054
  • 7
  • 44
  • 67
0

So far you have two pointers, each of which points to the first item of an array of structs.

If you want an array of T then you use the T[] notation, it is no different with pointers than with other types. So

struct inventoryItem *foo[10];
foo[0] = malloc( size * sizeof *foo[0] );

etc. In this example, inventory is replaced by foo[0]. You could of course then write struct inventoryItem *inventory = foo[0];

Of course you can also dynamically allocate foo if the number of arrays you need is not known at compile time:

struct inventoryItem **foo = malloc( 10 * sizeof *foo );
M.M
  • 138,810
  • 21
  • 208
  • 365