Himanshu Singh Chauhan wrote:
Can anybody tell what variable sized structures are and how can they be
used?
It allows for data to be accessible immediately after the structure
itself -- without being separately allocated. Notice, the code below doing
all allocation in a single malloc().
struct meow {
unsigned int size;
struct woof[];
} Meow;
....
static struct meow *
allocate_meow(unsigned int size)
{
struct meow *result;
result = malloc(sizeof(struct meow) + sizeof(struct woof) * size);
if (result == NULL)
return NULL;
result->size = size;
return(result);
}
......
if ((meows = allocate_meow(number)) == NULL)
errx("can not allocate %ud woofs", number);
for (i = 0; i < number; i++)
DO SOMETHING WITH meows.woof[i];
......
How about this?
-mi
--
OMG Pademelons!
http://en.wikipedia.org/wiki/Tasmanian_Pademelon
--
comp.lang.c.moderated - moderation address:
cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.