Himanshu Singh Chauhan said:
Hello All!!
Can anybody tell what variable sized structures are and how can they be
used?
Some data items, like names and postal addresses, are naturally
variable-sized.
So it would make sense to offer a variable-sized structure.
Unforunately C doesn't support this. It has to know the size of the struct
at compile time.
However there are workarounds. The good way is to say
struct customer
{
char *name;
char *address
};
Then allocate the members with malloc().
The nasty way, but one you will sometimes see, is
struct hackedcustomer
{
char name[20];
char address[0];
};
The "name" member is fixed at twenty bytes. However we can then put this
structure in an arbitrary bit of memory, and extend the "address" member
downwards. So address is flexible.
This isn't what I or most regs would recommend, but Microsoft use it for
some of their Windows structures, and it didn't do their bank balance any
harm.