0

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};
8
  • For one, that's unsigned not usigned, and then the braces around the value are wrong. Commented Jan 9, 2021 at 15:45
  • OT: arr[8] = {0XCF}; is out of bounds. Legal indices are 0 .. 7 Commented Jan 9, 2021 at 15:53
  • You may want to use std::array<uint8_t,8> arr; instead of unsigned char arr[8]; Commented Jan 9, 2021 at 15:56
  • 2
    Conversion to unsigned char is the least of your programs, here. char arr[8]={ ... } is a declaration and initialization. but arr[8]=something, whatever the "..." assigns a value to the ninth element of the array, just like arr[0]=something assigns 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. Commented Jan 9, 2021 at 16:02
  • 1
    After declaration of a variable (arr) anything else you do to it is no longer initialization. Commented Jan 9, 2021 at 16:14

2 Answers 2

1

I'm afraid that initializer syntax you are trying to use is only possible for variable declarations.

Possible solution are to declare a new array and copy it using memcpy

#include <iostream>
#include <cstring>

int main()
{
    unsigned char arr[4] =  {0x0F, 0x0F, 0x0F, 0x0F};
    if (elsecondition){
        unsigned char arr1[4] = {0xFF, 0xFF, 0xFF, 0xFF};
        memcpy(arr, arr1, sizeof(arr));
    }
return 0;
}

or to do the assignment one element at a time

#include <iostream>

int main()
{
    unsigned char arr[4] =  {0x0F, 0x0F, 0x0F, 0x0F};
    if (elsecondition){
        // we can use a loop, since all values are identical
        for (int i = 0; i < sizeof(arr); ++i)
            arr[i] = 0xFF;
    }
return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

First of: arr[4] = {0x0F, 0x0F, 0x0F, 0x0F}; will try to assign something to the 5th element of the array (but the types are not compatible and it would be an out-of-bounds-access).

This syntax is only availble in array initialization, however std::array overloads operator= and supports what you want:

std::array<unsigned char, 4> arr; 
if (...){
    arr = {0x0F, 0x0F, 0x0F, 0x0F};
} else {
    arr = {0xFF, 0xFF, 0xFF, 0xFF};
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.