By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,878 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

why arrary with size of zero?

P: n/a
I saw some library code wrote like this:

struct X{
int x;
char y[0]; /* Note: here array size is ZERO, why? */
};


Why use array size zero. I heard about the C standard
not allow array with size zero, but my gcc tell me I can
compile this code without error or warning.

One give me explaination this trick can used for dynamic
memory, for example:
size_t bufsize;
...
struct X* px = NULL;
px = (struct X*)malloc(sizeof(struct X) + bufsize);
in this way, we can gain a X and additional bufsize in once
malloc invoke. Does this correct?

Another question is whether this code portable?

Any help is appreciated.

--
Regards.
Mockey Chen
Email: mo*********@gmail.com
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Mockey Chen <mo*********@google.com> wrote:
struct X{
int x;
char y[0]; /* Note: here array size is ZERO, why? */
};
It's a nonportable hack, at best. It is almost, but not quite, the
"struct hack" that is also nonportable but nevertheless widely used.

http://www.eskimo.com/~scs/C-faq/q2.6.html
Why use array size zero. I heard about the C standard
not allow array with size zero, but my gcc tell me I can
compile this code without error or warning.


You're not invoking it in strictly conforming mode. Use gcc -Wall
-ansi -pedantic and you will see that it does, indeed, warn you that
it is a nonstandard construct.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 21 '05 #2

P: n/a
On 2005-11-21 09:43:29 -0500, Christopher Benson-Manica
<at***@nospam.cyberspace.org> said:
Mockey Chen <mo*********@google.com> wrote:
struct X{
int x;
char y[0]; /* Note: here array size is ZERO, why? */
};


It's a nonportable hack, at best. It is almost, but not quite, the
"struct hack" that is also nonportable but nevertheless widely used.

http://www.eskimo.com/~scs/C-faq/q2.6.html


Though, note that with C99, the following is legal (basically a
legalization of the struct hack):

#include <stdlib.h>

struct X {
int x;
char y[]; //Note empty brackets
};

struct X *allocateX(int count)
{
struct X *result = malloc(count + sizeof *result);
if(result)
{
result->x = count;
}
return result;
}

int main()
{
struct X *x_ptr;

if(x_ptr = allocateX(20))
{
//I can treat x_ptr->y basically as if it were an array of 20 char's
x_ptr->y[19] = 'a';
}

return 0;
}

Why use array size zero. I heard about the C standard not allow array
with size zero, but my gcc tell me I can compile this code without
error or warning.


You're not invoking it in strictly conforming mode. Use gcc -Wall
-ansi -pedantic and you will see that it does, indeed, warn you that
it is a nonstandard construct.

--
Clark S. Cox, III
cl*******@gmail.com

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.