1

I want to copy a long long array to an integer array using memcpy(). The size of the long long array is half of the size of the integer array.

Other than memcpy() I've used this:

int *dst;
long long src[10];
dst=(int*)src;

But I want to use only memcpy().

Because the purpose is, the long long array is a temporary array, which copies its contents, to the sub arrays of source array. The source array is a 2 dimensional array .

2
  • 1
    I'm almost sure you don't want to do that..but then ,.... Commented Jul 3, 2015 at 7:17
  • Your example does not even do a copy, but just a pointer cast. Commented Jul 3, 2015 at 7:44

1 Answer 1

3

In general, sizeof (long long) > sizeof (int).

So you cannot copy all elements in an array of long long into an array of int using a byte-by-byte copy operation such as memcpy(); the data won't fit.

You can use a loop, to manually do the (truncating) copy. This will of course lose information:

const long long incoming[] = { 1ll, 2ll, /* more here ... */ };
int out[sizeof incoming / sizeof *incoming];
for(size_t i = 0; i < sizeof incoming / sizeof *incoming; ++i)
  out[i] = (int) incoming[i];
Sign up to request clarification or add additional context in comments.

Comments

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.