phanitej
phanitej

Reputation: 113

copying a long long array to integer array in c language

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 .

Upvotes: 1

Views: 435

Answers (1)

unwind
unwind

Reputation: 399881

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];

Upvotes: 3

Related Questions