How to copy an array of int
to an array of char
:
int iarray[32];
char carray[sizeof(iarray)];
memcpy(carray, iarray, sizeof(iarray));
If carray
is a pointer instead of an array, the code remains largely the same:
int iarray[32];
char *carray = addressOfSomeMemory;
memcpy(carray, iarray, sizeof(iarray));
However, the code above assumes your data is stored as char
s packed into int
s. If instead your data is stored one char
per int
, you need more code:
for (int i = 0; i < 32; i++)
*(carray+i) = (char)iarray[i];