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

zero lengh arrays.

P: n/a
hi,
How to use Zero lengh Arrays..
I searched but could not get much information regarding this
where Do i get links abt this

Jun 16 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
$hiv..... schrieb:
How to use Zero lengh Arrays..
I searched but could not get much information regarding this
where Do i get links abt this


Zero length arrays cannot be explicitly declared
in standard C. As they make no sense in all but the
most esoteric contexts, you are better off without
them.

<OT> Your compiler may have an extension to enable
struct {
....
sometype array[0];
}
to simplify applying the so-called "struct hack" or
something similar.
This is off-topic around here, so you need to read
your compiler's documentation or ask in a newsgroup
for your compiler.
You still cannot really "use" the zero length array.
</OT>

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 16 '06 #2

P: n/a

$hiv..... wrote:
hi,
How to use Zero lengh Arrays..
I searched but could not get much information regarding this
where Do i get links abt this


example:

struct tag {
int tag_type;
int tag_len;
char tag_data[0];
} ;

int sample_tag_len = 10;
struct tag *sample_tag = (struct tag *)malloc(sizeof(struct
pppoe_tag)+sizeof(char)*sample_tag_len);
sample_tag->tag_type = 0xffff;
sample_tag->tag_len = sample_tag_len;
sample_tag->tag_data[0]=....
....

free(sample_tag);

Jun 16 '06 #3

P: n/a

seni.yin wrote:
$hiv..... wrote:
hi,
How to use Zero lengh Arrays..
I searched but could not get much information regarding this
where Do i get links abt this


example:

struct tag {
int tag_type;
int tag_len;
char tag_data[0];
} ;

int sample_tag_len = 10;
struct tag *sample_tag = (struct tag *)malloc(sizeof(struct
pppoe_tag)+sizeof(char)*sample_tag_len);
sample_tag->tag_type = 0xffff;
sample_tag->tag_len = sample_tag_len;
sample_tag->tag_data[0]=....
...

free(sample_tag);


Would it make any difference if you simply declared tag_data[] to
have size 1 ?

Jun 16 '06 #4

P: n/a
sp****@gmail.com wrote:
seni.yin wrote:
example:

struct tag {
int tag_type;
int tag_len;
char tag_data[0];
} ;

int sample_tag_len = 10;
struct tag *sample_tag = (struct tag *)malloc(sizeof(struct
pppoe_tag)+sizeof(char)*sample_tag_len);
sample_tag->tag_type = 0xffff;
sample_tag->tag_len = sample_tag_len;
sample_tag->tag_data[0]=....
...

free(sample_tag);


Would it make any difference if you simply declared tag_data[] to
have size 1 ?


Yes; the code would then be marginally more conformant. Fix the malloc()
call while you're at it.

For the right way to do this, read the FAQ:
<http://c-faq.com/struct/structhack.html>.

Richard
Jun 16 '06 #5

P: n/a
<sh********@huawei.com> wrote:
How to use Zero lengh Arrays..
...


See this thread:
http://groups.google.com/group/comp....5b1fc37446bd82
Jun 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.