How can I dynamically allocate the size of an arry at runtime
When I am doing this am getting an error..

#include<conio.h>
#include<stdio.h>
void main()
{
    int n;
    a[n];
    clrscr();

    printf("Enter no of your array\n");
    scanf("%d",&n);
    printf("Enter your array");
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

    for(i=0;i<n;i++)
    {
        printf("%d\n",a[i]);
    }
}

On line five you create an int named n, which could have any value. Any value at all. Could be anything.

On line six, you try to use an object named a, which you have not yet created. Are you trying to create an array there?

Here is hpow to create an array of int, named a.

int a[some_number];

If you want to dynamically create the aray, on the heap, use malloc.

int* a = malloc (sizeof(int) * some_number));

In your code, remember to create the array AFTER the user has provided a value for n.

Remember to free the memory when you don't need it anymore.

Edited 2 Years Ago by Moschops

C99 accepts variable length arrays on the stack. If you don't want to use malloc, just put it on the stack.

#include<stdio.h>
int main()
{
    int n;


    printf("Enter no of your array\n");
    scanf("%d",&n);
    int a[n];
    printf("Enter your array");
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

    for(i=0;i<n;i++)
    {
        printf("%d\n",a[i]);
    }
}

Edited 2 Years Ago by Moschops

For C++, there isn't much of a problem. With C, it is preferable to use malloc/realloc to resize arrays. The nice thing about realloc is that that data in the old array will end up in the new one. You only need to zero out the new elements. I have used this in C++ code for resizable collections of stuff, to great effect.

Note that in Moschops example, the elements of a[n] are not initialized and can be any value held by an int, which in this case is whatever is in memory on the stack at the time of construction. He uses scanf() to initialize the members, but it is usually appropriate (and safer) in such a case to simply zero out the array members before use. IE, memset((void*)a, 0, sizeof(a));

If 0 is a valid value, then use -1 for the value, as in memset((void*)a, -1, sizeof(a));

Edited 2 Years Ago by rubberman

Thankx for the help
C99 concept of variable array is not accepted in Recent Compilers .They give errors like" Constant Expressions required in Function main on line 9"

But after little modification Every thing went correctly by Initializing a dummpy value to an array and making it as global.
And then defining its value at runtime

#include<conio.h>
#include<stdio.h>

//Global Declaration of array with dummy value
int a[1];   

void main()
{
    int n;

    //Initializing the length of array at Runtime
    a[n];

    clrscr();

    //Accepting the length of array
    printf("Enter no of your array\n"); 
    scanf("%d",&n);

    //Accepting Array
    printf("Enter your array");
        for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

    //Displaying Array
    for(i=0;i<n;i++)
    {
        printf("%d\n",a[i]);
    }
}

Edited 2 Years Ago by dendenny01

C99 concept of variable array is not accepted in Recent Compilers .They give errors like" Constant Expressions required in Function main on line 9"

Then you're not compiling as C99. The compiler also must support C99, which not all recent compilers do. Further, it looks like you're using Turbo C, which is not a recent compiler by any measure.

However, that said, I don't recommend this feature even if it's supported (for various reasons I won't elaborate on unless asked). I'd recommend using a pointer and dynamic memory allocation.

But after little modification Every thing went correctly by Initializing a dummpy value to an array and making it as global.

Your code is broken. At the point of the array definition, n is uninitialized, which invokes undefined behavior.

Thanks A lot for your Assistance and helping me to understand various aspects.
And using pointer and Dynamic memory Allocation is far better than Variable Array Initalization

Edited 2 Years Ago by dendenny01

This question has already been answered. Start a new discussion instead.