numerical25
numerical25

Reputation: 10790

does memcpy params have to be of the same type?

I was reading that memcpy takes the number of bytes from a source location and adds it to a destination location. Does this mean that memcpy could possibly change datatype entirely ??

memcpy(DoubleOne, CharTwo, strlen(CharTwo));

considering that both values are empty still.

Upvotes: 1

Views: 289

Answers (2)

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 248199

Yes, memcpy doesn't care about the types. (It converts both its parameters to void pointers anyway)

It doesn't "change datatype" as much as it just writes char data into a double array (in your case) and hopes it makes sense.

Upvotes: 5

VDVLeon
VDVLeon

Reputation: 1394

Yes, they dont have to.

int test = 3;
char dest[sizeof(int)];

memcpy(&dest[0], &test, sizeof(int));

Is valid c(++).

Upvotes: 3

Related Questions