0

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.

My main problem is, how do I create an array with size based on the user input? This is what I have so far:

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", &size);
    struct inventoryItem inventory[size]; //problem here

}

I am fairly new to programming in C so a solution that is not too complex would be appreciated.

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.

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); //not sure if this is right

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

    //stuck here

    //struct inventoryItem *header = inventory; //error here
    for(int i = 0; i < size; i++)
    {
        //indexArray[i] = inventory[i];
    }

}
1
  • You should read this and especially this section Commented Mar 30, 2014 at 20:58

5 Answers 5

5

Assuming your "problem" is that the compiler objects, if you're using GCC or Clang, try adding the flag -std=c99 or -std=c11 to your command line. GCC defaults to an older version of the C language that doesn't have this functionality.

You don't need malloc unless you intend to return the array. Always use the simplest thing that will work.

Sign up to request clarification or add additional context in comments.

2 Comments

VLA:s are optional in C11.
It's also possible to use alloca().
4

You should use malloc to dynamically allocate the size of array

#include <stdlib.h>

int main()
{
    struct inventoryItem *inventory; //use pointer to item 
    printf("Enter the number of slots needed in the array: ");
    int size;
    scanf("%d", &size);

    //after you get the size input 
    inventory = malloc(sizeof(struct inventoryItem)*size);
}

In the end you should use the free to free the memory

2 Comments

If you don't want to repeat the typename then you can also write: inventory = malloc(size * sizeof *inventory); - this form lets you verify at a glance that you've allocated the right number of bytes.
Ok, thanks for info (I am usually using typedef but this 1 seems cooler :)
0

You need to use dynamic memory allocation to do that...

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

Hope it helped..

Comments

0

In C, you need to manage the memory yourself.

There are 2 ways to accomplish your goal:

  • dynamic memory allocation

    Like others already pointed out, use malloc. But don't forget to use free to release the memory when not needed anymore:

    inventory =(struct inventoryItem *) malloc(sizeof(struct inventoryItem)*size);
    /* start working */
    /* when you are done, release the memory */
    free(inventory);
    
  • static memory allocation

    Although generally not preferred, it is sometimes needed to statically initiate a space in memory. But do know, that this memory is forever reserved for storing a specified limited amount of your objects. In your case, where you give total freedom to the user to specify the amount of objects, this is probably not the best solution.

    struct inventoryItem
    {
        int itemNumber;
        float cost;
        float retailPrice;
        int itemsInStock;
    }
    
    struct inventoryItem itemlist[20];
    
    int main()
    {
        printf("Enter the number of slots needed in the array: ");
        int size;
        scanf("%d", &size);
        /* start working on the first <size> objects */
    
    }
    

Comments

0

Like others pointed, you can also do it with calloc function.This is also a one of the types of dynamic memory allocation and you can use this in the following way:

Inventory=(struct inventoryItem *)calloc(sizeof(struct inventoryItem),size);

Don't forget to free up the memory after your work:

free(inventory);

Comments

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.