4

So I have the following:

int from[2][3] = { {1,2,3}, {2,3,4} };
int into[3];

into = memcpy(into, from[0], 3 * sizeof(*into));

I want to copy 'from' in to the array 'into' so that 'into' = { 1, 2, 3}

I am trying to do the above using memcpy (i know that it already works with a loop) but I cant seem to get it working.

I keep on getting the error :

error: incompatible types when assigning to type ‘int[3]’ from type ‘void *’

I found a link to this question:

How do I copy a one-dimensional array to part of another two-dimensional array, and vice-versa?

and changed my code (Above) but i still get the error.

I am still clueless, I have solved my problem in another manner but curiosity I would like to know how it is done as from the previous post I know it is possible.

2 Answers 2

3

As KingsIndian points out, you can avoid the problem by dropping the assignment, since you don't actually need the return value in this instance. However it may help for the future to understand what's going on under the hood:

memcpy returns a pointer to its destination. If "into" were a pointer, then it would be fine:

int from[2][3] = { {1,2,3}, {2,3,4} };
int into[3];
int *into_ptr = into;

into_ptr = memcpy(into_ptr, from[0], 3 * sizeof(int)); // OK

The problem is that "into" is an array, not a pointer. Arrays in C are not variables, i.e. they cannot be assigned to, hence the error. Although it's often said that arrays and pointers are equivalent, there are differences, this being one. More detail on the differences between arrays and pointers is given here:

http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arrays-equivalent-in-c/

Edit:

To avoid the problem altogether by not doing any assignment, ignore the return value:

int from[2][3] = { {1,2,3}, {2,3,4} };
int into[3];

memcpy(&into[0], from[0], sizeof(into));
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, that worked and made a lot of sense too! One more add on question could I do it without needing to have int into[3] and int *into_ptr. It sounds a bit silly so I'm going with no but what I mean is that do i need the pointer to the array or can i do it with an array (store it in something else i guess) so i dont need the extra variable .... long winded question i hope you understand me
You do need to declare the array, since if you just have a pointer you'll need to independently allocate the memory. However the pointer can be eliminated if you ignore the return value per KingsIndian's answer.
I didnt get it working with KingsIndian's answer ... maybe I misread/understood what did he mean/ how do you mean what parts of the code change
Answer updated with complete code for doing it without intermediate pointer.
thanks that worked .. and i understnad that memcpy(into, ... would work as well because its identical to &into[0]
2

memcpy returns a pointer to the destination which you are trying to assign to an array. You can ignore the return value of memcpy.

   #include <string.h>

   void *memcpy(void *dest, const void *src, size_t n);

What you probably want is:

memcpy(into, from[0], sizeof into);

This will copy the 3 elements of 4 bytes each ( sizeof into == 12 here) from from[0] to into.

6 Comments

Explain why you can throw away the return value.
@djechlin: RTFM. "RETURN VALUE The memcpy() function returns a pointer to dest."
@thejh dude. I know how to read a manual. I also know C. It is good information to include in an answer. Someone who doesn't know how memcpy works is probably in the process of learning C and really shouldn't be left under the impression that it's okay to throwaway/ignore the return value half the time. Most return values tell you whether the function succeeded. memcpy is an exception and it would be an improved answer to note why.
I tried the above, it still deosnt work. Function ‘main’: test.c:9:8: error: incompatible types when assigning to type ‘int[3]’ from type ‘void *’
@user2426194 I think you are trying to assign the return value of memcpy() to into.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.