My understand has always been that when I declare an array on the stack with a size that comes in as a variable or parameter, I should get an error.
However, I noticed that I do not get any error if I do not explicitly initialize the array (yes, it won't be on the stack, but I'm wondering about the lack of error). For example, the following code does not compile because of array2:
#define N 30
void defineArrays(int n)
{
int i,j;
int array1[N] = {};
int array2[n] = {};
for(i=0; i<N; ++i) array1[i] = 0;
for(j=0; j<n; ++j) array2[j] = 0;
}
But the following code compiles and runs, even when I send a real n from main:
#define N 30
void defineArrays(int n)
{
int i,j;
int array1[N] = {};
int array2[n];
for(i=0; i<N; ++i) array1[i] = 0;
for(j=0; j<n; ++j) array2[j] = 0;
}
What I am missing here? Is it declaring array2 as a pointer? I'm using gcc
Update: Thanks for everyone who answered. The problem was indeed that my version of gcc was defaulting to C99 for some strange reason (or not so strange, maybe I'm just too old), and I incorrectly assumed that it defaults to C90 unless I tell it otherwise.