adding an integer to a char array in c - Printable Version +- 0Day Forums (https://zeroday.vip) +-- Forum: Coding (https://zeroday.vip/Forum-Coding) +--- Forum: C & C++ (https://zeroday.vip/Forum-C-C) +--- Thread: adding an integer to a char array in c (/Thread-adding-an-integer-to-a-char-array-in-c) |
adding an integer to a char array in c - sedan698 - 07-27-2023 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 RE: adding an integer to a char array in c - oriole905 - 07-27-2023 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. RE: adding an integer to a char array in c - zeburro876714 - 07-27-2023 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`. RE: adding an integer to a char array in c - diplocarpon124430 - 07-27-2023 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][1] 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: - [byte order][2] in integers.; - [data alignment][3]; [1]: [To see links please register here] [2]:[To see links please register here] [3]:[To see links please register here] RE: adding an integer to a char array in c - abomine736190 - 07-27-2023 According to the documentation of [`memcpy()`]( [To see links please register here] ) :> void * memcpy ( void * destination, const void * source, size_t num ); Copies the values of `num` bytes from the location pointed by `source` directly to the memory block pointed by `destination`. In your case : - `num`=`sizeof(int)` - `destination`=`&test` A pointer to test - `source`=`&array[4]` A pointer to the fourth element of the array of char `array` Hence, if `sizeof(int)==4` it will copy `array[4]`, `array[5]`,`array[6]` and `array[7]` to `test` There are questions that can help you understand the memory layout of integers : - [To see links please register here] -[To see links please register here] There is also an issue with endianless : on my computer, `array[4]` corresponds to the least significant byte. Consequently, if `array[7]=0x80` and `array4]=array[5]=array[6]=0x00` then `test` will contain 00000080 and `test` will worth -2^31. if `array[7]=0x2A` and `array[5]=array[6]=array[4]=0x00` then `test` will contain 2A000000 and `test` will worth 42 (that is 0x0000002A). Here is a test code to be compiled by `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; } RE: adding an integer to a char array in c - humbuzz601189 - 07-27-2023 Generally the C library function **void *memcpy(void *str1,const void *str2,size_t n)** copies **n** characters from memory area **str2** to memory area **str1**, where: **str1** – this is pointer to the destination array where the content is to be copied, type-casted to a pointer of type void* **str2** -- this is pointer to source of data to be copied, type-casted to a pointer of type void* **n** -- this is the number of bytes to be copied memcpy returns a pointer to destination, which is str1 In your case, is copied the contents of the array, from the address pointed to by array[4] up to sizeof (int) bytes (4 bytes in this case, if you have a 32bit machine), the address pointed to by test |