It may be silly question but still am not getting it. I do have a char array say char arr[100] having some data
char arry[100] ---- some data;
int test;
memcpy(&test,array+4,sizeof(int))
What does this memcpy will do Thanks SKP
This might be useful in so-called serialization of data.
Say, if someone saved an integer into a file.
Then you read the file into a buffer (arry
in your case) as a stream of bytes. Now you want to convert these bytes into real data, e.g. in your case integer test
which has been stored with offset 4.
There are several ways to do that. One is to use memcpy to copy bytes into area where compiler would treat them as an integer.
So to answer your question:
memcpy(&test,array+4,sizeof(int))
...will copy sizeof(int) number of bytes, starting from 4-rth byte from array
into memory allocated for variable test
(which has type int
). Now test
has the integer value which was saved into arry
originally, probably using the following code:
memcpy(array+4, &original_int, sizeof(int))
Doing this requires some knowledge of hardware and the language. As there are many complications, among which:
It just copy the element array[4]
to variable test
. On 32-bit machine sizeof(int)
= 4
. memcpy
will copy 4
bytes to the address &test
which can hold 4
bytes.
This will copy probably 4 bytes (depending on your machine and compiler--your int
might be bigger or smaller) from the 4th through 7th bytes of arry
into the integer test
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With