memcpy():
void * memcpy ( void * destination, const void * source, size_t num );
num , source, , destination.
:
num= sizeof(int)destination= &testsource= &array[4] char array
, sizeof(int)==4 array[4], array[5], array[6] array[7] test
, :
endianless: array[4] .
, array[7]=0x80 array4]=array[5]=array[6]=0x00, test 00000080 test -2 ^ 31.
if array[7]=0x2Aand array[5]=array[6]=array[4]=0x00, then it testwill contain 2A000000 and testwill cost 42 (i.e. 0x0000002A).
Here is the test code that should be compiled gcc main.c -o main
#include <stdio.h>
#include <string.h>
int main(int argc,char *argv[]){
char array[100];
int test;
printf("sizeof(int) is %ld\n",sizeof(int));
array[4]=0x00;
array[5]=0;
array[6]=0;
array[7]=0x80;
memcpy(&test,&array[4],sizeof(int));
printf("test worth %d or(hexa) %x\n",test,test);
array[4]=0x2A;
array[5]=0;
array[6]=0;
array[7]=0x00;
memcpy(&test,&array[4],sizeof(int));
printf("test worth %d or(hexa) %x\n",test,test);
return 0;
}
source
share