title: C User input size of Array
body: <p>I am trying to write a program that can create an array with size based on the user input and then store structs inside.
  The struct will contain two ints and two floats.</p>
  
  <p>My main problem is, how do I create an array with size based on the user input?
  This is what I have so far:</p>
  
  <pre><code>struct inventoryItem
  {
      int itemNumber;
      float cost;
      float retailPrice;
      int itemsInStock;
  }
  
  
  int main()
  {
      printf("Enter the number of slots needed in the array: ");
      int size;
      scanf("%d", &amp;size);
      struct inventoryItem inventory[size]; //problem here
  
  }
  </code></pre>
  
  <p>I am fairly new to programming in C so a solution that is not too complex would be appreciated.</p>
  
  <p>EDIT:
  So now that I have the first part solved, I am now trying to create a second array that holds pointers to the first array's data (an index).
  Now the problem is I do not know how to create a for loop that can take the pointers to the first array and store them into the second.
  I declared the indexArray as type 'int' and am not sure if that is right.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>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", &amp;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); //not sure if this is right
  
      //fill array contents
      for(int i = 0; i &lt; size; i++)
      {
          printf("Enter item %d number: ", i);
          scanf("%d", &amp;inventory[i].itemNumber);
  
          printf("Enter item %d stock: ", i);
          scanf("%d", &amp;inventory[i].itemsInStock);
  
          printf("Enter item %d cost: ", i);
          scanf("%f", &amp;inventory[i].cost);
  
          printf("Enter item %d price: ", i);
          scanf("%f", &amp;inventory[i].retailPrice);
      }
  
      for(int i = 0; i &lt; 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);
      }
  
      //stuck here
  
      //struct inventoryItem *header = inventory; //error here
      for(int i = 0; i &lt; size; i++)
      {
          //indexArray[i] = inventory[i];
      }
  
  }
  </code></pre>