Theocacao
Leopard
Design Element
Comment on "Intro to Memory and Arrays in C"
by Martin — Oct 17
Scott,

In your example of a variable length array, even though you set the size with a random number, you don't actually exceed the original length you specify. So :-

int stackArray[COUNT];

and

int stackArray[rand()];

are the same at runtime. To demonstrate a variable length array shouldn't you assign a value to the array passed the end of the array when it is setup? like in the following code :-
#include #include #define COUNT 5 main() { srand(time(NULL)); int stackArray[COUNT]; int i; for(i=0; i< COUNT;i++){ stackArray[i] = rand(); } for(i=0;i
In a version C that does not support variable length arrays as soon as you try to assign a value in this case to stackArray slot 6 it would crash with a "Segmentation fault"

Your thoughts,

Martin
Design Element

Copyright © Scott Stevenson 2004-2015