I have a complex structure which looks like this.
struct a
{
struct b
{
int b_inner_int;
char b_inner_char;
}x;
struct c
{
int c_inner_int;
char c_inner_char;
}y;
}z;
I use a function, that takes address of "struct c" as an argument. Now I want this function to copy the values of "struct c" to "struct b". The function call that I make in the main function may look like this.
copy_val(&z.y);
Now, how do I define copy_val? Any suggestions? If i define a pointer of type struct c, like below it isn't working.
void copy_val(struct c *addr)
{
struct c *tmp=addr;
int tmp_int=tmp->c_inner_int;
int tmp_char=tmp->c_inner_char;
tmp=tmp-1; /** assuming that b and c are of same type and decrementing pointer by 1 takes to beginning of b **/
tmp->b_inner_int=tmp_int;
tmp->b_inner_char=tmp_char;
}