zero lengh arrays.

$

$hiv.....

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

Michael Mair

$hiv..... said:
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
 
S

seni.yin

$hiv..... said:
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);
 
S

spibou

seni.yin said:
$hiv..... said:
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 ?
 
R

Richard Bos

seni.yin said:
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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top