Set array size at runtime

21,720

Solution 1

May be you like this:

typedef struct item 
{
    int test; 
};

item *arr;

void func (void)
{
    int arraysize = 5;
    arr = calloc(arraysize,sizeof(item)); 
     // check if arr!=NULL : allocation fail!
     // do your work 

    free(arr);
}

But its dynamic allocation!

And if arraysize known at compilation time. then better to create a macro like this:

#define arraysize  5

typedef struct item 
{
    int test; 
};

item arr[arraysize];  

side note using upper case for macro constants is good practice

Solution 2

Variable length arrays are only allowed in C for arrays with automatic storage duration. Arrays declared at file scope have static storage duration so cannot be variable length arrays.

You can use malloc to dynamically allocate memory for an array object whose size is unknow at compilation time.

Solution 3

item * ar:
int count;

void foo()
{
    count = 5;
    ar = malloc(sizeof(item) * count);
    // check to make sure it is not null...
}

Solution 4

typedef struct item 
{
    int test; 
};

#define ARRAYSIZE 5

item ar[ARRAYSIZE];
Share:
21,720
Maestro
Author by

Maestro

Updated on January 13, 2020

Comments

  • Maestro
    Maestro over 4 years

    I know how to create a structure array inside a function:

    typedef struct item
    {
        int test;
    };
    
    void func (void) 
    {
        int arraysize = 5;
        item ar[arraysize];  
    }
    

    But how do I to the same when the array is declared globally:

    typedef struct item 
    {
        int test; 
    };
    
    item ar[];
    
    void func (void)
    {
        int arraysize = 5;
        // What to here?
    }
    
  • Maestro
    Maestro over 11 years
    I dont want to modify the size of an existing (initialized) array, but to specify the size for an uninitialized array. Or doesnt that make any difference for C?
  • Maestro
    Maestro over 11 years
    Would be possible to create the variable array inside the function, and have a global pointer to it, or does the array going out of scope make this impossible?
  • Jack
    Jack over 11 years
    @Joshua: Makes no any difference in C. See my edit. malloc() is your solution.
  • ouah
    ouah over 11 years
    @Joshua this has nothing to do with scope but with storage duration. The variable length array can go out of scope it is ok, but you should not exit the function where it is declared. When you exit the function where the array is declared, the object is discarded.
  • Maestro
    Maestro over 11 years
    That is only possible when 5 is known at compile-time.
  • Jim Rhodes
    Jim Rhodes over 11 years
    Your int arraysize = 5; implies that the size IS known at compile time.
  • Jim Rhodes
    Jim Rhodes over 11 years
    @LeeTaylor I know the difference. His code has arraysize = 5. That IS compile time.
  • Lee Taylor
    Lee Taylor over 11 years
    @JimRhodes - Perhaps clarity is needed on the question title Set array size at runtime...