6

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.

asked Mar 30, 2009 at 19:46

4 Answers 4

14

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

answered Mar 30, 2009 at 19:50
Sign up to request clarification or add additional context in comments.

2 Comments

Damn, I haven't realized gcc defaults to c99 now. I've been using g++ for too long.
@Uri No +1 on your question, because of your curseword.
3

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
answered Mar 30, 2009 at 19:51

Comments

1

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'.

answered Mar 30, 2009 at 19:51

1 Comment

I understand that, but why am I able to compile the second one?
1

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

answered Mar 30, 2009 at 19:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.