|
P: n/a
|
nileshsimaria@gmail.com
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 | |
Share this Question
|
P: n/a
|
Richard Heathfield
nileshsimaria@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. | | |
P: n/a
|
nileshsimaria@gmail.com
Richard Heathfield wrote:
nileshsimaria@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.
| | |
P: n/a
|
Cong Wang
nileshsimaria@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);
}
" | | |
P: n/a
|
Richard Heathfield
nileshsimaria@gmail.com said:
>
Richard Heathfield wrote:
> nileshsimaria@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. | | |
P: n/a
|
Tom St Denis
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 | | |
P: n/a
|
Cong Wang
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. | | |
P: n/a
|
Richard Tobin
In article <1164806203.416069.220160@80g2000cwy.googlegroups. com>,
Tom St Denis <tomstdenis@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. | | |
P: n/a
|
Jean-Marc Bourguet
Richard Heathfield <rjh@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 | | |
P: n/a
|
Richard Heathfield
Jean-Marc Bourguet said:
Richard Heathfield <rjh@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. | | |
P: n/a
|
CBFalconer
nileshsimaria@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> | | |
P: n/a
|
CBFalconer
nileshsimaria@gmail.com wrote:
Richard Heathfield wrote:
> nileshsimaria@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> | | |
P: n/a
|
Ben Pfaff
CBFalconer <cbfalconer@yahoo.comwrites: nileshsimaria@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 | | |
P: n/a
|
Keith Thompson
nileshsimaria@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) kst-u@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. | | |
P: n/a
|
Peter Nilsson
CBFalconer wrote:
nileshsimaria@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 | | |
P: n/a
|
Keith Thompson
"Peter Nilsson" <airia@acay.com.auwrites:
CBFalconer wrote:
> nileshsimaria@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) kst-u@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. | | |
P: n/a
|
Peter Nilsson
Keith Thompson wrote:
"Peter Nilsson" <airia@acay.com.auwrites:
CBFalconer wrote:
nileshsimaria@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 | | |
P: n/a
|
Harald van Dijk
Peter Nilsson wrote:
Keith Thompson wrote:
"Peter Nilsson" <airia@acay.com.auwrites:
CBFalconer wrote:
> nileshsimaria@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.) | | |
P: n/a
|
Peter Nilsson
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 | | |
P: n/a
|
Harald van Dijk
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". | | Post your reply Help answer this question
Didn't find the answer to your C / C++ question?
You can also browse similar questions: C / C++ | | Question stats - viewed: 1364
- replies: 19
- date asked: Nov 29 '06
|