H
Heriberto Bens
Hi, friends.
I've seen the following code in an example:
int fun(int n)
{
int i, x[n];
for (i = 0; i < n; i++)
x = 5;
for (i = 0; i < n; i++)
printf("%i\n", x);
}
main()
{
fun(10);
}
As you can see, array x size depends on the argument of the function fun.
I thought this was not possible as local variables are located in the
stack, and the compiler should know what size they are going to take.
I have compiled the code and seems to work ok, but I don't know if it's
correct and will always run ok.
Is it possible to make this kind of things? If it is, is
it better to work like that rather than using malloc()?
Thanks a lot.
I've seen the following code in an example:
int fun(int n)
{
int i, x[n];
for (i = 0; i < n; i++)
x = 5;
for (i = 0; i < n; i++)
printf("%i\n", x);
}
main()
{
fun(10);
}
As you can see, array x size depends on the argument of the function fun.
I thought this was not possible as local variables are located in the
stack, and the compiler should know what size they are going to take.
I have compiled the code and seems to work ok, but I don't know if it's
correct and will always run ok.
Is it possible to make this kind of things? If it is, is
it better to work like that rather than using malloc()?
Thanks a lot.