Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 517 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
adding an integer to a char array in c

#1
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
Reply

#2
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.
Reply

#3
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`.
Reply

#4
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]

Reply

#5
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;
}
Reply

#6
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
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through