if (sizeof (int) == sizeof (short)) {
array2 = (int*)array1; // UB: this may not work on some implementations
} else {
// allocate memory for array2
for (i = 0; i < nelems; i++) {
array2[i] = array1[i];
}
// possibly release resources for array1
}
How do I cast array of unsigned short to an array of int?
Domanda
I have 2 arrays:
unsigned short* array1;
int* array2;
Is there a quick way to get all the data in array1 into array2 such that the values of array2[0]=array1[0], array2[1] = array1[1]
...etc.
My question mentions casting but I think that's wishful thinking! Obviously I can do this element by element using a for loop but I'm writing an image processing application and speed is key. If not casting then is there any other solution? Thanks.
Soluzione 2
Altri suggerimenti
How do I cast array of unsigned short to an array of int?
You don't. If you do, and you use the result, you'll have some nice undefined behavior at hand.
Is there a quick way to get all the data in array1 into array2
Yes, copy the array elements. It will be fast on any decent modern computer.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow