vendredi 27 février 2015

C: Copying Pointer data using = (Equal to)



This is my first post here. I tried to find a similar question but couldn't find any and hence I am posting this question.


I am trying to copy the data in a structure pointer to another structure pointer in C as shown below. Looks like using just an '=' did the trick. Could someone please explain how the data in pointer srcStruct got copied to another memory location (pointer by destStruct) without using memcpy()?


Is this a pointer property? or is there any risk in doing this?



struct myTestStruct
{
short variable1;
short variable2;
};
struct myTestStruct *destStruct = (struct myTestStruct *)malloc(sizeof(struct myTestStruct));
struct myTestStruct *srcStruct = (struct myTestStruct *)malloc(sizeof(struct myTestStruct));
srcStruct->variable1 = 11;
srcStruct->variable2 = 12;
*destStruct = *srcStruct;


Thanks in advance.


Regards, AJK




Aucun commentaire:

Enregistrer un commentaire