Copying a struct to a larger struct?
- From: "hermes_917@xxxxxxxxx" <kgarrison@xxxxxxxxx>
- Date: 27 Jul 2005 12:32:08 -0700
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.
--
Karl Garrison
hermes_917@xxxxxxxxx
.
- Follow-Ups:
- Re: Copying a struct to a larger struct?
- From: CBFalconer
- Re: Copying a struct to a larger struct?
- From: Barry Schwarz
- Re: Copying a struct to a larger struct?
- From: Michael Mair
- Re: Copying a struct to a larger struct?
- From: tedu
- Re: Copying a struct to a larger struct?
- Prev by Date: Re: Hints on how to migrate from C++ to C
- Next by Date: Re: Hints on how to migrate from C++ to C
- Previous by thread: Question on arrays within a struct and sprintf
- Next by thread: Re: Copying a struct to a larger struct?
- Index(es):