I'm new in C programming and would love to get some help.
I have this struct :
typedef struct house
{
int numOfRooms;
char* houseName;
}HOUSE,*pHOUSE;
I want to create a function that gets a pointer to HOUSE and returns a new pointer to same HOUSE that is located in a different place in memory - the purpose is to be able to change one pointer without changing both : I will try to be clearer :
pHOUSE duplicate_house(pHOUSE house)
{
pHOUSE newh = (pHOUSE)calloc(1,sizeof(HOUSE));
newh = house
//I get here a pointer that points on the same house.. so If I change for exmample:
// newh->numOfRooms = 9 - > both will change and I don't want it to happen!
}
I read that we can use : memcpy_s
but here If I only had integers inside the struct it could be easy , here I have char * , so means I need also to copy the char * houseName
separately?
what can I do? How can I copy an object that has multiply types like char *
?
and If I had an array ? what could I do ?
typedef struct house
{
int numOfRooms;
char* houseName;
struct house *houses[10];
}HOUSE,*pHOUSE;
how can I copy that?
thank u very much!
You need to copy both the structure as well as all memory that's managed by the structure. Like so:
As you can see, the error handling with two allocations is already getting very cumbersome. If you had multiple internal allocations, the only way to remain sane is to use
goto
s systematically to create the appropriate cleanup points.Example to illustrate the last point: