Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How does variable sized array work?

+1 vote
asked Sep 22 by John (130 points)
So I have 2 programs, program 1 defined a variable sized array in main() and the other in a separate function. The array in program 1 actually gets compiled while 2 results in an error.

The way I understand this is the the compiler needs to know the array's size at compile time so that it could allocate memory, but I have no idea why this doesn't apply to program 1.

//Program 1
void main(){
    int i;
    scanf("%d",&i);
    int arr[i];
}

//Program 2
void new(int n){
    int arr[n];
}

void main(){
    new(10);
}

1 Answer

0 votes
answered Sep 22 by Peter Minarik (34,450 points)
I took both of your code snippets (Program 1 and Program 2), compiled and ran. Both ran, no problems.

Note: variable-sized arrays are not supported under Microsoft platform. But OnlineGDB runs on Linux, so we're good.

So, I don't understand your question. I don't see any problems in either Program 1 or Program 2.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...