473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Array of zero element

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

Nov 29 '06 #1
19 7044
ni***********@gmail.com 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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 29 '06 #2

Richard Heathfield wrote:
ni***********@gmail.com 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,
>
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 29 '06 #3

ni***********@gmail.com wrote:
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[i] = letter++;
printf("sizeof(vlen)=%d\n",sizeof(vlen));
for(i=0; i<line->size; i++)
printf("%c ",line->string[i]);
printf("\n");
return(0);
}
"

Nov 29 '06 #4
ni***********@gmail.com said:
>
Richard Heathfield wrote:
>ni***********@gmail.com 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

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 29 '06 #5
Cong Wang wrote:
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

Nov 29 '06 #6

Tom St Denis wrote:
Cong Wang wrote:
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.
Of course I know that's NOT portable. I *just* want to explain what
zero length array is and how it is used.
>
int count = 22;
char letter = 'a';
vlen *line = (vlen *)malloc(sizeof(vlen) + count);
line->size = count;

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.

Nov 29 '06 #7
In article <11**********************@80g2000cwy.googlegroups. com>,
Tom St Denis <to********@gmail.comwrote:
>"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.
>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

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 29 '06 #8
Richard Heathfield <rj*@see.sig.invalidwrites:
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,

--
Jean-Marc
Nov 29 '06 #9
Jean-Marc Bourguet said:
Richard Heathfield <rj*@see.sig.invalidwrites:
>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?
Good spot! How did that get in there? :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Nov 29 '06 #10
ni***********@gmail.com wrote:
>
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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 29 '06 #11
ni***********@gmail.com wrote:
Richard Heathfield wrote:
>ni***********@gmail.com 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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 29 '06 #12
CBFalconer <cb********@yahoo.comwrites:
ni***********@gmail.com wrote:
>>
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".
--
"The expression isn't unclear *at all* and only an expert could actually
have doubts about it"
--Dan Pop
Nov 29 '06 #13
ni***********@gmail.com writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 29 '06 #14
CBFalconer wrote:
ni***********@gmail.com wrote:
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,
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. ..."

--
Peter

Nov 30 '06 #15
"Peter Nilsson" <ai***@acay.com.auwrites:
CBFalconer wrote:
>ni***********@gmail.com wrote:
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,

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
[...]

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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 30 '06 #16
Keith Thompson wrote:
"Peter Nilsson" <ai***@acay.com.auwrites:
CBFalconer wrote:
ni***********@gmail.com wrote:
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
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.
>
switches to make it one.
Well, it moves it closer.
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
[...]

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

--
Peter

Dec 2 '06 #17
Peter Nilsson wrote:
Keith Thompson wrote:
"Peter Nilsson" <ai***@acay.com.auwrites:
CBFalconer wrote:
>ni***********@gmail.com wrote:
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

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

Dec 2 '06 #18
Harald van Dijk wrote:
Peter Nilsson wrote:
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.
I didn't know that. Thanks.
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.)
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++.

--
Peter

Dec 5 '06 #19
Peter Nilsson wrote:
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.)

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

Dec 5 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bart Nessux | last post by:
The below code works well, except for the database insert. I want each element of the array to be inserted into the database, but only the last element of the array is inserted... all the elements...
33
by: Zytan | last post by:
I want to make a zero element array. I know that Nothing is not the same as a zero element array, since I can't get the length of, or iterate through, an array = Nothing. I could make a zero...
1
by: peachdot | last post by:
hi, i want to create an array element of increment value in a loop. example: a= output of my code : a=
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.