Page 1 of 1
struct has an illegal zero-sized array
Posted: Fri Oct 08, 2010 3:26 am
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.
Re: struct has an illegal zero-sized array
Posted: Fri Oct 08, 2010 4:07 am
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.
Re: struct has an illegal zero-sized array
Posted: Fri Oct 08, 2010 4:22 am
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)?
Re: struct has an illegal zero-sized array
Posted: Fri Oct 08, 2010 4:47 am
by shahadat
i was stripping the nessesary file so that i can add it to my os project
Re: struct has an illegal zero-sized array
Posted: Tue Oct 12, 2010 2:51 am
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];
}
Re: struct has an illegal zero-sized array
Posted: Tue Oct 12, 2010 4:44 am
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).
Re: struct has an illegal zero-sized array
Posted: Tue Oct 12, 2010 6:46 am
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.
Re: struct has an illegal zero-sized array
Posted: Wed Oct 13, 2010 12:06 am
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
Re: struct has an illegal zero-sized array
Posted: Wed Oct 13, 2010 2:59 am
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'