Archxiao
Archxiao

Reputation: 199

Are empty arrays no longer allowed by gcc?

I have gcc 4.7.2-3 and I get this following error:

main.c:5:6: error: array size missing in ‘fname’

main.c:6:6: error: array size missing in ‘lname’

when initializing this:

int fname[];
int lname[];

Is this no longer possible using higher versions of gcc? Because I am certain I have used this before...

EDIT: The reason I say I remember this, is because I even see it here: http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V40F_HTML/AQTLTBTE/DOCU_046.HTM

Upvotes: 1

Views: 3168

Answers (4)

Melvin Kumar
Melvin Kumar

Reputation: 1

Yes, you can declare an array with 0 size. To do so you need to declare as mentioned below

int array[] = {};

For example

int main()
{
    int a[]={};
    printf("size = %d", sizeof(a));
    return 1;
}

./a.out gives you 0 as output.

Upvotes: 0

Boumbles
Boumbles

Reputation: 2523

You can only declare an array with no size if you are initializing it right away

int myarr[] = {1, 2, 3};

or if it is the last member in a structure

struct foo {
  int something;
  int somethingelse;
  char emptyarr[];
};

Upvotes: 2

user405725
user405725

Reputation:

Array of zero length can be either a GNU C extension or a flexible array member introduced in C99 (which C++ compilers also happen to support and I am not sure if it is in C++ standard or not). In either case, those are allowed as a last member of a structure only and otherwise are not allowed and do not make any sense either.

The document that you refer to does use or show "empty" arrays. You are confusing array decaying for "empty" arrays.

Upvotes: 1

Eugene
Eugene

Reputation: 7258

I don't think it was ever possible in any compiler, array size must be known at compile time. You were probably doing initialization right away:

int fname[] = { 1, 2, 3};

This way compiler can derive the size.

EDIT: Ah, missed the "C" tag. :) It seems to be illegal in C++ though.

Upvotes: 0

Related Questions