struct has an illegal zero-sized array

Programming, for all ages and all languages.
Post Reply
shahadat
Posts: 19
Joined: Thu May 13, 2010 2:53 am

struct has an illegal zero-sized array

Post by shahadat »

when i was trying to compile ntfs-3g, i find the error message.
it seems that zero size array is not allowed in some structs in msvc 8.
or may be i m wrong.
i googled the problem.
but never find any solution.
can anybody help me getting around this.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: struct has an illegal zero-sized array

Post by JamesM »

Zero-sized arrays are usually variable-sized arrays used for serialization to and from disk, with their length specified in a preceding field.

Because of their variable-sized-ness, they usually appear at the end of a struct. It is therefore usually safe to replace them with an array size of 1, instead of 0. This is unless the sizeof(struct) is taken, in which case there is no safe replacement and the MSVC warning must be disabled.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: struct has an illegal zero-sized array

Post by pcmattman »

Hmm, why are you compiling ntfs-3g with MSVC? Doesn't Windows already have an NTFS driver (considering it's been around since at least 2003, and is used by XP, Vista and 7)?
shahadat
Posts: 19
Joined: Thu May 13, 2010 2:53 am

Re: struct has an illegal zero-sized array

Post by shahadat »

i was stripping the nessesary file so that i can add it to my os project
shahadat
Posts: 19
Joined: Thu May 13, 2010 2:53 am

Re: struct has an illegal zero-sized array

Post by shahadat »

djgpp support zerro sized array in structure.
is there any way to force msvc to support zerro sized array.

well,
msvc works fine on the following:

struct abcd
{
int a;
int b;
int c;
char d[0];
}

but when i do the following it show the error
struct abcd
{
int a;
char b[0];
int c;
char d[0];
}
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: struct has an illegal zero-sized array

Post by NickJohnson »

IIRC, the C99 standard only allows zero-sized arrays at the end of structures, because they're supposed to be used to add a variable length array to the end of a structure. It's reasonable that MSVC would reject the second example, not to mention that abcd.b == &(abcd.c), so having b is redundant and useless (unless you're trying to make a union of b and c or something).
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: struct has an illegal zero-sized array

Post by Owen »

C99 variable length arrays should be declared "Type name[]" anyway. [0] is a deprecated GCC extension.

Not that this helps you. You're trying to compile C99 code on MSVC, which doesn't support it.
shahadat
Posts: 19
Joined: Thu May 13, 2010 2:53 am

Re: struct has an illegal zero-sized array

Post by shahadat »

i found a new way.
the following works fine.


typedef struct
{
int a;
union
{
char b[1];
unsigned int c;
};
char d[0];
} abcd;

thanks to everybody for replying
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: struct has an illegal zero-sized array

Post by qw »

Are anonymous unions standard C? I can't find it in the standard and I am finding different answers on the web.

EDIT: To answer my own question, according to GCC they aren't. I tried to compile the following code:

Code: Select all

struct foo
{
    union
    {
        long l;
        float f;
    };
};

struct foo bar;

long test1(void)
{
    return bar.l;
}

float test2(void)
{
    return bar.f;
}
With the following results:

Code: Select all

gcc -c -std=gnu99 -pedantic test.c
test.c:8: warning: ISO C doesn't support unnamed structs/unions
test.c:9: warning: struct has no named members

Code: Select all

gcc -c -std=c99 -pedantic test.c
test.c:8: warning: ISO C doesn't support unnamed structs/unions
test.c:8: warning: declaration does not declare anything
test.c:9: warning: struct has no members
test.c: In function `test1':
test.c:15: error: structure has no member named `l'
test.c: In function `test2':
test.c:20: error: structure has no member named `f'
Post Reply