Having already read vjefcoat's post on structure copy i would like (as my first post in this forum) to open a discussion on the issue of structure copying.
Lest assume we have two structures like this
Code:
typedef struct list1{
int data1;
int data2;
struct list1 *next;
struct list1 *previous;
} dounlenode; //this is a node for a double linked list
typedef struct list2{
int data1;
int data2;
struct list1 *next;
} singlenode; //this is a node for a sinlgy linked list
As far as i ve read (i only found it in just one book!) you can copy two structures as simply as:
Code:
doublenode dn1,dn2;
dn1=dn2; //this is legal and works like a charm
I also tried this with pointers
Code:
doublenode *dnptr1,*dnptr2;
//...
//mallocs for the pointers etc.etc.
//...
*dnptr1=*dnptr2;
i was also amazed that the following works
Code:
doublenode *dnptr1;
singlenode *sinptr1;
//...
//mallocs for the pointers etc.etc.
//...
*dnptr1=*((*doublenode)sinptr1);
this is a cast of the single list node into a double list node (of which the structure is "bigger") and i copied the contents of the single list node into the double linked node. It worked ok.
What troubles me are the following:
1)Is this a good practice?
2)Is the success of such an assignement machine or compiler dependant?
3)What happens when the sizes/formats of the structures are different (i think you get a conversion to non scalar type error) Is there a way arround this?
Your opinions are most welcome. Thank you