store contents of structure into an array



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]);

}
.



Relevant Pages

  • Re: Init.c, making it chroot
    ... that's located at a special place inside the chroot. ... int devfs; ... typedef struct init_session { ... main(int argc, char *argv) ...
    (freebsd-hackers)
  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)
  • Re: Connector - how to start?
    ... It seems like one of the Red Hat guys had some netlink documentation ... Netlink is fast but not faster than char device for example, ... static int need_exit; ... struct nlmsghdr *nlh; ...
    (Linux-Kernel)
  • Re: size_t in a struct
    ... "If you're worried about wasted space, you can minimize the effects of ... I should have mimized padding since size_t is an unsigned long. ... Testing three different compilers I happen to have here, I can have 16-bit, 32-bit and 64-bit pointers to char. ... typedef struct _Buffer2_t { ...
    (comp.lang.c)
  • [PATCH]a tar filesystem for 2.6.10-rc1-mm3
    ... +static int tarfs_readdir(struct file * filp, ... struct tarent *dir_tarent, *ent; ... +static int tarfs_readlink(struct dentry *dentry, char *buffer, int buflen) ...
    (Linux-Kernel)