# Thread: Casting an array problem from unsigned int to uint8_t

1. ## Casting an array problem from unsigned int to uint8_t

Hi guys, Im facing a serious problem and I need your help please!
what Im facing is that Im getting value from another function as integer
which they are :
Code:
`{5,1,2,3,4,5}`
so those value exactly I want to copy them to an array type uint8_t.
this means the array that I want to get is
Code:
`uint8_t arr={5,1,2,3,4,5};`
what actually the problem?
the problem is this if I want to initialize and array type uint8_t with those values then I write immediately in the compiler:
Code:
`uint8_t arr={5,1,2,4,5};`
(I mean by intilizing immediately ..once I write my syntax uint8_t arr in my compiler I write immediately
Code:
`{5,1,2,3,4,5}`
)
the problem that those values Im getting them from other function ..and I want those values to be entered as it's in an array type uint8_t this means my array would be the same with those value but type uint8_t =>
Code:
`uint8_t arr={5,1,2,3,4,5};`
how can I do that ? I tried to use unsigned int and starting filling it with values
Code:
` {5,1,2,3,4,5}`
but it didn't work!

any help how can I implement that? thanks alot.

what I mean by code is this:
Code:
```int array2={5,1,2,3,4,5};
unit8_t array1={0};
for(int i=0;i<(sizeof(array2)/sizeof(array2));i++)
{
array1[i]=array[i];} /*I want the same values of array2 to enter to array1 but the type of array to be uint8_t*/
}```
so what I expect to get once I print output is the array1 which it should be :
Code:
` {5,1,2,3,4,5}`
and array1 type uint8_t with the same values that I copied them to it.

but the output is wrong answer, it'sï¿½ï¿½ï¿½aï¿½ï¿½ß™ï¿½ï¿½Iï¿½ï¿½ 2. Maybe the problem lies with the printing of the output.

I suggest that you post the smallest and simplest compilable program that demonstrates this problem. 3. Perhaps the problem is that
Code:
`int array2={5,1,2,3,4,5};`
is not an array

Code:
`int array2[]={5,1,2,3,4,5};`
might work better 4. This is a bit much, but the following might help you to better understand whatever it is you're working with.

Code:
```#include <cstdint>
#include <vector>

void arrays() {
// this is an integer initialized to 7
int a = 7;

// this is an array of 4 integers uninitialized
int b;

// this is an array of 3 integers initialized to 1, 2, and 7
int c = {1, 2, a};

// this is an array of 3 integers initialized to 1, 2, and 7
int d[] = {1, 2, a};

// this is an array of 4 integers initialized to 1, 2, 7, and 0
int e = {1, 2, a};

// this is an array of 3 integers initialized to 0, 0, and 0
int f = {};

// error: array must be initialized with a brace-enclosed initializer
// int g = c;

// error: initializer fails to determine size of ‘h’
// char h[] = e;

// error: scalar object ‘i’ requires one element in initializer
// int i = {1, 2, 3};
}

// this is a function that takes a pointer to an int, which the function will
// then interpret as the beginning of an array of `length` ints
void pointer_style(int* first, int length) {
// an array of 32 uninitialized uint8_t
std::uint8_t bytes;

// can you spot the bug?
for (int i = 0; i < length; ++i) {
// this looks like "array syntax," but really it's pointer arithmetic
bytes[i] = first[i];
// also, an int can store more distinct values than a uint8_t can, so
// this is a "narrowing conversion"
}
}

// this is a function that takes a reference to an array of 32 ints
void reference_style(int (&array)) {
// an array of 32 uninitialized uint8_t
std::uint8_t bytes;

for (int i = 0; i < 32; ++i) {
bytes[i] = array[i];
}
}

// this is a function that takes a vector of ints
void standard_style(std::vector<int> numbers) {
// bytes is initialized using the values in numbers
std::vector<std::uint8_t> bytes(numbers.begin(), numbers.end());

// now, bytes.size() == numbers.size()
}

int main() {
// nothing to see here
}``` Popular pages Recent additions array, problem, type, uint8_t, values 