I am trying to declare and initialize a unsigned char arr within if else block, but im seeing " cannot convert '' to 'unsigned char' in assignment" error. Can anyone please help me understand whats wrong with this? I am new to c++.
Edited:
unsigned char arr[4];
if (..){
arr[4] = {0x0F, 0x0F, 0x0F, 0x0F};
} else {
arr[4] = {0xFF, 0xFF, 0xFF, 0xFF};
}
Going the below way doesn't have any issue. But I need assignment happening inside if-else so am trying to understand whats the problem with above snippet?
unsigned char arr[4] = {0xFF, 0xFF, 0xFF, 0xFF};
unsignednotusigned, and then the braces around the value are wrong.arr[8] = {0XCF};is out of bounds. Legal indices are 0 .. 7std::array<uint8_t,8> arr;instead ofunsigned char arr[8];char arr[8]={ ... }is a declaration and initialization. butarr[8]=something, whatever the "..." assigns a value to the ninth element of the array, just likearr[0]=somethingassigns the value to the first element in the array. The problem, of course, is that there is no ninth element in an eight-element array. Any attempt to do so will result in demons flying out of your nose. If you don't want demons flying out of your nose you should not do this.arr) anything else you do to it is no longer initialization.