1

I require some help with code that throws a 'Warning array subscript has type 'char' [-Wchar-subscripts]' message.

The code in question is:-

static
const long hextable[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10-19
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 30-39
    -1, -1, -1, -1, -1, -1, -1, -1, 0, 1,
    2, 3, 4, 5, 6, 7, 8, 9, -1, -1, // 50-59
    -1, -1, -1, -1, -1, 10, 11, 12, 13, 14,
    15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 70-79
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, // 90-99
    13, 14, 15, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 110-109
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 130-139
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 150-159
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 170-179
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 190-199
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 210-219
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 230-239
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1
};

long hex2long(const char* hexString)
{
    long ret = 0;
    while (*hexString && ret >= 0)
    {
        ret = (ret << 4) | hextable[*hexString++];
    }
    return ret;
}

Now it appears that the line ret = (ret << 4) | hextable[*hexString++]; has issues.

I am hoping someone can assist me re-structuring this code to prevent the error.

9
  • What is hextable and what are you trying to achieve with *hexString++? Commented Nov 10, 2019 at 21:59
  • I have updated question to include hextable. Commented Nov 10, 2019 at 22:01
  • 1
    change to hextable[(uint8_t)*hexString++] Commented Nov 10, 2019 at 23:28
  • another problem is that ret << 4 will cause undefined behaviour after a while, depending on the string content (it is undefined to shift a 1 into the sign bit) Commented Nov 10, 2019 at 23:35
  • Thank you so much @M.M - simply changing hextable[(uint8_t)*hexString++] seems to have corrected the issue. Commented Nov 10, 2019 at 23:46

1 Answer 1

4

Change const char* to const unsigned char*.

From GCC Warnings:

-Wchar-subscripts Warn if an array subscript has type char. This is a common cause of error, as programmers often forget that this type is signed on some machines. This warning is enabled by -Wall.

The compiler doesn't want you to use negative indices for the array, so it throws a warning to let you know of this potential problem.

Sign up to request clarification or add additional context in comments.

4 Comments

Changing the parameter type of hex2long may not be feasible.
@1201ProgramAlarm then you need to assign/cast *hexString to an unsigned char before using it as an array index.
Re: “ it will overflow to negative” — maybe. The behavior when a signed type overflows is undefined.
@PeteBecker there's no overflow, this answer just used incorrect terminology. Which was not even the original answerer's text, it was edited in by someone else. I've removed the offending statement (the quoted section already covers that anyway)

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.