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
 
M

Malcolm

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.
 
V

Valerio

Mmmm.. well, i'm not sure to understand what you're meaning; it comes
in my mind struct sockaddr. it's a structure to represent a logical
socket address:
t's declaration is

struct sockaddr {
sa_family_t sa_family;
char sa_data[14];
};

but it's never used as is. Infact if you need a unix socket you declare
a struct sockaddr address but you really use a struct sockaddr_un:
struct sockaddr_un {
sa_family_t sun_family; /* AF_UNIX */
char sun_path[UNIX_PATH_MAX]; /* pathname */
};

But if you need an inet (internet) socket you declare a struct sockaddr
address but you really use a struct sockaddr_in:

struct sockaddr_in {
sa_family_t sin_family; /* address family: AF_INET */
u_int16_t sin_port; /* port in network byte order
*/
struct in_addr sin_addr; /* internet address */
};

struct sockaddr_in has different size respect the other two.

Generally speaking, if you need a struct with different sizes you can:
Create a "skeleton" struct A, then create others struct for every
possible size;
every one of these must have the first (maybe second, third ecc..)
field, same for all the structs. The first field specify the struct
type therefore its effective size.

In C often you've a pointer to data but you don't know how long data
are: in a struct with the first field you know data lenght too.

bye,

Valerio
 
D

Denis Kasak

Malcolm said:
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.

Actually, C99 does. The feature is called 'flexible array member'. It
offers the possibility of declaring the last member of a struct as an
array with an indeterminate number of elements, like this:

struct foo {
int bar;
int qux[];
}

The downside is that such a struct cannot be a member of another struct
itself, nor can you have an array of such structs.
 
T

those who know me have no need of my name

[fu-t set]

in comp.lang.c i read:
The nasty way, but one you will sometimes see, is

struct hackedcustomer
{
char name[20];
char address[0];
};

since this is a constraint violation you usually see address as a single
element array, which is either ignored (and the resulting allocation is one
byte larger than needed) or offsetof is used to obtain the size of the
object up to address or the desired size is reduced by 1 manually -- the
first form (ignore it) allows the code to remain clean and tends to match
what would be done with c99 fam's, though the second form (use offsetof)
also works with c99 fam's.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top