Array of zero element

N

nileshsimaria

Hi,

I have seen some code were we have array with zero elements (mostly
within structure)

like,

typedef struct foo {
int data[0]
}foo;

Just curious whats the use of this kind of code ? And how to use member
variable 'data' of struct foo ?

Thanks,
Nilesh
 
R

Richard Heathfield

(e-mail address removed) said:
Hi,

I have seen some code were we have array with zero elements (mostly
within structure)

like,

typedef struct foo {
int data[0]
}foo;

Just curious whats the use of this kind of code ?

It's good for generating diagnostic messages, such as:

warning: ANSI C forbids zero-size array `data'
warning: no semicolon at end of struct or union
And how to use member
variable 'data' of struct foo ?

Make it a big bigger than 0, stick a semicolon on the end, instantiate it,
and then use the member operator . to access the member object.
 
N

nileshsimaria

Richard said:
(e-mail address removed) said:
Hi,

I have seen some code were we have array with zero elements (mostly
within structure)

like,

typedef struct foo {
int data[0]
}foo;

Just curious whats the use of this kind of code ?

It's good for generating diagnostic messages, such as:

warning: ANSI C forbids zero-size array `data'
warning: no semicolon at end of struct or union

Ok, I put semicolon and compiling using gcc, its not producing warning,
 
C

Cong Wang

Hi,

I have seen some code were we have array with zero elements (mostly
within structure)

like,

typedef struct foo {
int data[0]
}foo;

Just curious whats the use of this kind of code ? And how to use member
variable 'data' of struct foo ?

Thanks,
Nilesh

Array of zero length is NOT standard C, but a GCC extension. In the
book "GCC - The Complete Reference", it is said that:
"GNU C allows the declaration of arrays of zero length to facilitate
the creation of
variable-length structures. This only makes sense if the zero-length
array is the last
member of a struct. The size of the array can be specified by simply
being allocated
the amount of space necessary. The following program demonstrates the
technique:
/* zarray.c */
#include <stdio.h>
typedef struct {
int size;
char string[0];
} vlen;
int main(int argc,char *argv[])
{
int i;
int count = 22;
char letter = 'a';
vlen *line = (vlen *)malloc(sizeof(vlen) + count);
line->size = count;
for(i=0; i<count; i++)
line->string = letter++;
printf("sizeof(vlen)=%d\n",sizeof(vlen));
for(i=0; i<line->size; i++)
printf("%c ",line->string);
printf("\n");
return(0);
}
"
 
R

Richard Heathfield

(e-mail address removed) said:
Richard said:
(e-mail address removed) said:
Hi,

I have seen some code were we have array with zero elements (mostly
within structure)

like,

typedef struct foo {
int data[0]
}foo;

Just curious whats the use of this kind of code ?

It's good for generating diagnostic messages, such as:

warning: ANSI C forbids zero-size array `data'
warning: no semicolon at end of struct or union

Ok, I put semicolon and compiling using gcc, its not producing warning,

Let's put the semicolon in, then, and re-compile using gcc:

warning: ANSI C forbids zero-size array `data'

If you're not getting this warning, I suggest you crank up your warning
level. To compile your code, I used my usual options:

gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
-Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
-Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
-Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
-Wno-conversion -ffloat-store -O2 -g -pg -c -o foo.o foo.c
 
T

Tom St Denis

Cong said:
Array of zero length is NOT standard C, but a GCC extension. In the
book "GCC - The Complete Reference", it is said that:
"GNU C allows the declaration of arrays of zero length to facilitate
the creation of
variable-length structures. This only makes sense if the zero-length
array is the last
member of a struct. The size of the array can be specified by simply
being allocated
the amount of space necessary. The following program demonstrates the
technique:

What a horrible unportable idea.
int count = 22;
char letter = 'a';
vlen *line = (vlen *)malloc(sizeof(vlen) + count);
line->size = count;

Why not just make string a "char*" ???

Tom
 
C

Cong Wang

Tom said:
What a horrible unportable idea.

Of course I know that's NOT portable. I *just* want to explain what
zero length array is and how it is used.
Why not just make string a "char*" ???

Tom

If I wrote the code, I will use char* too. But the code is picked from
that book, which, of course, is not written by me.
 
R

Richard Tobin

"GNU C allows the declaration of arrays of zero length to
facilitate the creation of variable-length structures. This only
makes sense if the zero-length array is the last member of a
struct. The size of the array can be specified by simply being
allocated the amount of space necessary.
[/QUOTE]
What a horrible unportable idea.

It's a technique that has been used in many programming languages,
probably since the 1950s. For example, it was a common way of
representing Lisp symbols, which had some fixed size parts (e.g. value
cell) and a variable-length name.

It's sufficiently common in C that is has a name: the "struct hack",
and has been incorporated into the language (with a different syntax)
in C99.
Why not just make string a "char*" ???

Because it requires an extra pointer, an extra indirection, and
doubles the number of allocations required[*], which may be very
significant in some applications.

[*] Actually in this case it would be straightforward to allocate
enough for the struct plus the string, and set the pointer
accordingly.

-- Richard
 
J

Jean-Marc Bourguet

Richard Heathfield said:
gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
-Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
-Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
-Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
-Wno-conversion -ffloat-store -O2 -g -pg -c -o foo.o foo.c

What's the aim of having both -Wconversion and -Wno-conversion?

Yours,
 
C

CBFalconer

I have seen some code were we have array with zero elements
(mostly within structure) like,

typedef struct foo {
int data[0]
}foo;

Just curious whats the use of this kind of code ? And how to use
member variable 'data' of struct foo ?

Only in C99 code, and only when data is the last field in the
struct foo. It is a means of legitimizing the "struct hack", which
you can google for. Alternatively, read the standard.
 
C

CBFalconer

Richard said:
(e-mail address removed) said:
I have seen some code were we have array with zero elements
(mostly within structure) like,

typedef struct foo {
int data[0]
}foo;

Just curious whats the use of this kind of code ?

It's good for generating diagnostic messages, such as:

warning: ANSI C forbids zero-size array `data'
warning: no semicolon at end of struct or union

Ok, I put semicolon and compiling using gcc, its not producing warning,

By default gcc is not a C compiler. You need at least

-W -Wall -ansi -pedantic

switches to make it one. Besides, there is no requirement for any
compiler to produce any particular warning.
 
B

Ben Pfaff

CBFalconer said:
I have seen some code were we have array with zero elements
(mostly within structure) like,

typedef struct foo {
int data[0]
}foo;

Just curious whats the use of this kind of code ? And how to use
member variable 'data' of struct foo ?

Only in C99 code, and only when data is the last field in the
struct foo. It is a means of legitimizing the "struct hack", which
you can google for. Alternatively, read the standard.

C99 uses [] instead of [0] for its legitimate version of the
struct hack, which is by the way called a "flexible array
member".
 
K

Keith Thompson

I have seen some code were we have array with zero elements (mostly
within structure)

like,

typedef struct foo {
int data[0]
}foo;

Just curious whats the use of this kind of code ? And how to use member
variable 'data' of struct foo ?

It's a form of the "struct hack". It's explained in question 2.6 of
the comp.lang.c FAQ, <http://www.c-faq.com/>. A more portable form
uses "int data[1];", but even that is of questionable legality. C99
replaces the struct hack with flexible array members, but not all
compilers yet support that feature.
 
P

Peter Nilsson

CBFalconer said:
By default gcc is not a C compiler. You need at least

-W -Wall -ansi -pedantic

switches to make it one. Besides,

Did you mean 'otherwise'?
there is no requirement for any
compiler to produce any particular warning.

5.1.1.3p1:

"A conforming implementation shall produce at least one diagnostic
message
(identified in an implementation-defined manner) if a preprocessing
translation
unit or translation unit contains a violation of any syntax rule or
constraint,
even if the behavior is also explicitly specified as undefined or
implementation-
defined. ..."
 
K

Keith Thompson

Peter Nilsson said:
Did you mean 'otherwise'?


5.1.1.3p1:

"A conforming implementation shall produce at least one diagnostic
message (identified in an implementation-defined manner) if
[...]

Yes, diagnostic messages are required in some circumstances; warnings
are not.
 
P

Peter Nilsson

It's not clear to me that -pedantic is actually required to make gcc
conforming.
AFAIK, it is possible to turn on _all_ gcc warnings (including required
diagnostics) without using -pedantic. The additional property that
-pedantic
will reject certain programs is not a requirement of C90, nor C99, with
the
exception of the #error in the latter case.

Well, it moves it closer.
Besides,

Did you mean 'otherwise'?


5.1.1.3p1:

"A conforming implementation shall produce at least one diagnostic
message (identified in an implementation-defined manner) if
[...]

Yes, diagnostic messages are required in some circumstances; warnings
are not.

Neither are fog-horns.

I think terms like diagnostics and constraint violations, which come
out of the
standards, are better than esoteric words like warning and error.
[That's
not to say that I don't ever use the latter, but I am trying more and
more to
note the difference between the vageries of the later and the precision
of
the former.]
 
G

Guest

Peter said:
It's not clear to me that -pedantic is actually required to make gcc
conforming.
AFAIK, it is possible to turn on _all_ gcc warnings (including required
diagnostics) without using -pedantic.

This is not correct. For example, you cannot get GCC to warn about
variable-length arrays in C90 mode without it.
The additional property that
-pedantic
will reject certain programs is not a requirement of C90, nor C99, with
the
exception of the #error in the latter case.

-pedantic does not cause certain programs to be rejected,
-pedantic-errors does. (There might be special exceptions, but
generally speaking, -pedantic is supposed to only cause extra warnings
to be issued.)
 
P

Peter Nilsson

Harald said:
This is not correct. For example, you cannot get GCC to warn about
variable-length arrays in C90 mode without it.

I didn't know that. Thanks.
-pedantic does not cause certain programs to be rejected,
-pedantic-errors does. (There might be special exceptions, but
generally speaking, -pedantic is supposed to only cause extra warnings
to be issued.)

Your parenthetical comment may be how most people use it, but rejection
of some programs is a clear facet of the option. From the online
manual:

-pedantic
Issue all the warnings demanded by strict ISO C and ISO C++; reject
all
programs that use forbidden extensions, and some other programs
that do
not follow ISO C and ISO C++.
 
G

Guest

Peter said:
Your parenthetical comment may be how most people use it, but rejection
of some programs is a clear facet of the option. From the online
manual:

-pedantic
Issue all the warnings demanded by strict ISO C and ISO C++; reject
all
programs that use forbidden extensions, and some other programs
that do
not follow ISO C and ISO C++.

This seems to be a documentation issue. For C code, extensions which
cause valid programs to be given a different meaning or rejected, which
I assume is what is meant by "forbidden extensions", are disallowed by
the -ansi or -std=* options (as documented for the -ansi option), not
by the -pedantic option. The other extensions (those that -pedantic
causes warnings for) are not forbidden: the C standard explicitly
allows extensions (4.6) as long as they do not "alter the behavior of
any strictly conforming program".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top