variable sized structures.

  • Thread starter Himanshu Singh Chauhan
  • Start date
H

Himanshu Singh Chauhan

Hello All!!

Can anybody tell what variable sized structures are and how can they be
used?

regards
--Himanshu
 
S

santosh

Himanshu said:
Hello All!!

Can anybody tell what variable sized structures are and how can they be
used?

You got a couple of answers. Did you look them over. What's the point
in posting multiple times. This is an Usenet group and not a chatroom
which you can keep spamming.
 
J

Jordan Abel

Hello All!!

Can anybody tell what variable sized structures are and how can they be
used?

The term probably refers to a struct whose last member is an array with
no number of elements

struct foo {
int bar;
char baz[];
}

Generally one would use it with malloc(sizeof(struct foo)+n) where n is
the size you want for the flexible member.
 
M

Mikhail Teterin

Himanshu said:
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;
......

How about this?

-mi
 
H

Himanshu Chauhan

santosh said:
You got a couple of answers. Did you look them over. What's the point
in posting multiple times. This is an Usenet group and not a chatroom
which you can keep spamming.

Thanks for your help. By the way, to err is humane. Haven't you ever
said, oops!! I clicked wrong button specially when using a mouse pad on
notebook? :) I am sorry but I had no intentions of spamming, had I had
any, this would have been multiples of multiples, ain't it?
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top