store contents of structure into an array
- From: n00dle0@xxxxxxxxx (n00dle)
- Date: 31 Mar 2005 07:27:23 -0800
hi,
see if i want to copy the contents of a char arrary into a equally
size struct, assuming that the structure has no holes, padding etc., i
can use
structure = *(t_structure *) array;
but if i want to do things the other way round i.e. copy contents of
the structure to an char array. do i specifically need to have a loop
to do so, or is there a more elegant way to do it.
in case i am not clear have a look at the code below.
tia,
ash
int main()
{
typedef struct
{
char name[16];
char address[48];
char good;
char bad;
char ugly;
char bug;
} t_source;
char dest[68];
char *pt_char;
int i;
t_source source;
source = *(t_source *) dest;
pt_char = (char *) &source;
source.good='g';
for (i=0;i<=67;i++)
{
dest[i]=*pt_char;
pt_char++;
}
printf ("source.bad=%c char[64]=%c", source.bad, dest[64]);
}
.
- Follow-Ups:
- Re: store contents of structure into an array
- From: Eric Sosman
- Re: store contents of structure into an array
- From: David Resnick
- Re: store contents of structure into an array
- Prev by Date: Re: What is memory pool
- Next by Date: Re: store contents of structure into an array
- Previous by thread: Re: detecting very long floats?
- Next by thread: Re: store contents of structure into an array
- Index(es):
Relevant Pages
|