'How to create an array without declaring the size in C?

I'm trying to create a int and a float array without a size (it might be 0 or it might increment while the user use the program).

I was trying to do the follow:

int bills[];

float totalAmount[];

I can't assign a max size because I'm printing each array with a for loop (If I assign a size of 99 I'll print 99 lines, and I don't want that).



Solution 1:[1]

You don't declare an array without a size, instead you declare a pointer to a number of records.

so, if you wanted to do

int bills[];

The proper way to do this in C is

int* bills;

And you will have to allocate the size at some point in time and initialize the array.

bills = (int*)malloc(sizeof(int)*items);

The same goes for arrays of other data types. If you don't know the size of the array until runtime, you should use pointers to memory that are allocated to the correct size at runtime.

Solution 2:[2]

You could use a combination of malloc() (or calloc()), realloc() and free() to achieve this.

Memory can be allocated as blocks of a fixed size rather than reallocating memory for each number to be stored.

Let's define a macro (or a const if you like) BLOCK_SIZE.

#define BLOCK_SIZE 10

First declare a pointer of appropriate type and allocate the first block.

Note that malloc() as well as realloc() return NULL if some error occurred due to reasons like insufficient memory.

int *ptr=malloc(sizeof(int)*BLOCK_SIZE);    
if(ptr==NULL)
{
    perror("some error");
    return 1;
}

Now declare a variable to store the maximum possible index as per the currently allocated memory (to avoid illegal memory access).

int max_index = BLOCK_SIZE-1;

Now use a loop.

for(int i=0; ; ++i)
{
    if(i > max_index)
    {
        ptr=realloc(ptr, (max_index+1 + BLOCK_SIZE)*sizeof(int));
        if(ptr == NULL)
        {
            perror("insufficient memory!");
            break;
        }
        printf("\nRealloced!");
        max_index += BLOCK_SIZE;
    }
    scanf("%d", &ptr[i]);
    printf("\n%d: %d", i, ptr[i]);
}

In each iteration, we check if i is greater than max_index. If it is, another block is allocated using realloc() before reading the value.

Don't forget to deallocate the memory once you are done using it.

free(ptr);

Also, as discussed in this post, malloc() is effectively the same as realloc() with the latter's first argument NULL.

And in the code you posted, there's no need to explicitly cast the return value of calloc() as what's returned is a void pointer which would implicitly be converted to the target pointer type.

See this and this.

Solution 3:[3]

i think you can give it a max size ,if you only want to show the first few elements you can put a a for loop upto that element only,same goes for input if u want to initiallize first 30 elements put a for loop upto 30.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mike65535
Solution 2
Solution 3 Abhay Jaggi