tags:

views:

1266

answers:

4

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.

+13  A: 

C99 introduced the ability to have variable length arrays which is now available in GCC (although it's reported as not being totally standards compliant). In the second example, you appear to be taking advantage of that functionality.

Link to GCC's info about variable length arrays: http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html

JaredPar
Damn, I haven't realized gcc defaults to c99 now. I've been using g++ for too long.
Uri
+3  A: 

I think that you need to choose you C standard version.

gcc -fsyntax-only -std=c89 -pedantic -x c -

<stdin>: In function ‘defineArrays’:
<stdin>:6: warning: ISO C forbids empty initializer braces
<stdin>:8: warning: ISO C90 forbids variable length array ‘array2’

vs.

gcc -fsyntax-only -std=c99 -pedantic -x c -
<stdin>: In function ‘defineArrays’:
<stdin>:6: warning: ISO C forbids empty initializer braces
Charles Bailey
+1  A: 

Declaring the array with an initializer forces the array to be static (created at compile time) even though the scope is within the function. The compiler cannot define the array at compile time because it does not then know the value of 'n'.

HankB
I understand that, but why am I able to compile the second one?
Uri
+1  A: 

When I compile the first example with gcc, it gives me this error:

error: variable-sized object may not be initialized

I imagine this is not allowed because you don't know how big n will be, and therefore you can't be sure that it will be big enough to hold all the elements you are trying to initialize. That is, suppose you have code like this:

int array2[n] = { 1, 2, 3, 4 };

This requires that array2 have (at least) 4 slots. What if n is passed in as zero?

The second example does not have this problem because you are not making any implicit statement about the size of array2.

Hope that helps,

Eric Melski

Eric Melski