C Puzzles

yet another place for C puzzles

Friday, July 07, 2006

 

char data[0]

typedef struct mall_li_header_ {
int refcnt;
uchar pool;
uchar flag;
ushort magic_no;
char data[0];
} mall_li_header_t;


What is the use of data[0] here ?

Comments:
I am not sure why this is mentioned as a puzzle ... this is a very old (and common) idea used by a lot of game programmers from yesteryears.

The answer for completeness sake - common use is : allocate arbitrary size - anything above the size of the struct can be referenced as "data".
 
Yes. This is not exactly a puzzle. But I myself puzzled when I saw this piece of code first time in workspace :-)

So just thought to log it.

Thanks for your visit. and could you please send c-puzzles to me.

Thank You,
YaSJ
 
This is a zero byte array. Used to append data of arbit size at runtime. Very useful in sending data over a socket.
-angavras
 
Post a Comment

Subscribe to Post Comments [Atom]





<< Home

Archives

July 2005   August 2005   October 2005   December 2005   March 2006   June 2006   July 2006   December 2006   February 2007   June 2007   March 2010   May 2010  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]