H
hermes_917
I want to use memcpy to copy the contents of one struct to another
which is a superset of the original struct (the second struct has extra
members at the end). I wrote a small program to test this, and it
seems to work fine, but are there any cases where doing something like
this could cause any problems?
Here's the small program I wrote to test this:
#include <stdio.h>
int main()
{
struct simplestruct
{
int i;
char str[8];
};
struct extendedstruct
{
int i;
char str[8];
int j;
};
struct simplestruct foostruct;
struct extendedstruct barstruct;
/* Populate the members of the simplestruct instance */
foostruct.i=7;
strcpy(foostruct.str, "Test");
/* Copy the contents of the simplestruct instance to the
extendedstruct instance */
memcpy(&barstruct, &foostruct, sizeof(foostruct));
/* Populate remaining member of the extendedstruct instance */
barstruct.j=13;
/* Print values of members of the extendedstruct instance */
printf("i\t%d\nstr\t%s\nj\t%d\n", barstruct.i, barstruct.str,
barstruct.j);
}
Thanks in advance for any advice.
which is a superset of the original struct (the second struct has extra
members at the end). I wrote a small program to test this, and it
seems to work fine, but are there any cases where doing something like
this could cause any problems?
Here's the small program I wrote to test this:
#include <stdio.h>
int main()
{
struct simplestruct
{
int i;
char str[8];
};
struct extendedstruct
{
int i;
char str[8];
int j;
};
struct simplestruct foostruct;
struct extendedstruct barstruct;
/* Populate the members of the simplestruct instance */
foostruct.i=7;
strcpy(foostruct.str, "Test");
/* Copy the contents of the simplestruct instance to the
extendedstruct instance */
memcpy(&barstruct, &foostruct, sizeof(foostruct));
/* Populate remaining member of the extendedstruct instance */
barstruct.j=13;
/* Print values of members of the extendedstruct instance */
printf("i\t%d\nstr\t%s\nj\t%d\n", barstruct.i, barstruct.str,
barstruct.j);
}
Thanks in advance for any advice.