1. ## Help memcpy!

Hi all, this is my first post and i'm a beginner in c programming,
i need help to understand what i'm doing wrong.

I'm not getting the expected result:

Code:
```...
size_t size = sizeof(uint32_t);
...
void *ret = malloc(size * width * height);
if (ret == NULL) return 1;
...
memcpy(ret + (y * width * size) + x, data + (y * width * size) + w, size);
...```
i'm getting the expected result through, with this piece of code:

Code:
```...
size_t size = sizeof(uint32_t);
...
void *ret = malloc(size * width * height);
if (ret == NULL) return 1;
...
((uint32_t *)ret)[y * width + x] = ((uint32_t *)data)[y * width + w];
...```
but as the size is dynamic, i can't use this, and i really need something like the fist case.

Thanks in advance, and sorry if my english is not the best.

2. What are the results in first case?

3. Originally Posted by Kozay Novel
Code:
```...
memcpy(ret + (y * width * size) + x, data + (y * width * size) + w, size);
...```
i'm getting the expected result through, with this piece of code:

Code:
```...
((uint32_t *)ret)[y * width + x] = ((uint32_t *)data)[y * width + w];
...```
Your offset is different between these two cases.For example in 1st case you write (y * width * size) + x but if you compare it with the second one,i believe that you wish to write (y * width + x )* size

4. My guess is (y * width + x ) * size is closer to [y * width + x].

Tim S.

5. > memcpy(ret + (y * width * size) + x, data + (y * width * size) + w, size);
Well this shouldn't even compile, since you can't do arithmetic on a void pointer.

It you make ret like this
unsigned char *ret;

then the address calculation you have should work with
(y * width + x ) * size

6. Thanks std10093 and stahta01,
Sorry but the in the original, i copy wrongly from a commentary in my code (as i try many offsets).

it should be:

Code:
```...
size_t size = sizeof(uint32_t);
...
void *ret = malloc(size * width * height);
if (ret == NULL) return 1;
...
memcpy(ret + (y * width) + x, data + (y * width) + w, size);
...```
and:

Code:
```...
size_t size = sizeof(uint32_t);
...
void *ret = malloc(size * width * height);
if (ret == NULL) return 1;
...
((uint32_t *)ret)[y * width + x] = ((uint32_t *)data)[y * width + w];
...```
and still does not work.

7. Thank Salem, it works now, but actually, it works with void* pointers, as it is to represent any type, that is why i needed to use as its is a dynamic array that can be uint32_t, uint64_t, or (sizeof(uint8_t)*3).

Code:
```...
size_t size = sizeof(uint32_t);
...
void *ret = malloc(size * width * height);
if (ret == NULL) return 1;
...
memcpy(ret + (y * width + x) * size, data + (y * width + w) * size, size);
...```

8. It might work with your compiler (today), but the fact remains that you can't rely on void* pointer arithmetic.
Try increasing the warning levels on your compiler.

\$ gcc -Wall -Wextra -ansi -pedantic foo.c
foo.c: In function ‘main’:
foo.c:11:13: warning: pointer of type ‘void *’ used in arithmetic [-pedantic]

9. Thanks Salem, i corrected my code now, through i normally use C99, it's a good call to remember to use, if possible, C89/90.

10. Originally Posted by Kozay Novel
through i normally use C99, it's a good call to remember to use, if possible, C89/90.
C99 does not define pointer arithmetic with void* either.