Home > Software design >  Why this code returns the following error after compiling "invalid type argument of unary '
Why this code returns the following error after compiling "invalid type argument of unary '

Time:01-24

Given a 24bit string included in three 8bit registers, say: array[0], array[1], array[2] where the first bit of array[0] is the MSB and the last bit of array[2] is the LSB. My goal is to convert this string in an integer value.

For this purpose I have written the following code:

uint32_t bin2Int(uint8_t array[]) {
    uint8_t arr0[3];
    uint8_t mask = 0x80;
    uint8_t outputData[24];
    uint8_t s;
    uint8_t k = 23;
    uint32_t IntResult = 0;
    for (s=0; s<3; s  ) {
      arr0[s] = *array[s];
      while (mask > 0) {
          outputData[k] = arr0[s] & mask;
          mask >>= 1;
          k--;
      }
      mask = 0x80;
    }
    for (s=0; s<24; s  ) {
      if(outputData[s] != 0)
          IntResult  = pow(2, s);
    }
    return IntResult;
}

This function is implemented in STM32CubeIde, when I build the project the compiler returns the following error: "invalid type argument of unary '*' (have 'int')". Before adding this code in my project I tested it in DevC ide and it works fine, but when I try it on STM32CubeIde it returns this error. What's wrong in my code? Thank you very much.

CodePudding user response:

In this statement

arr0[s] = *array[s];

the right hand side expression has the pointer type uint8_t * while the left hand side expression has the type uint8_t.

It seems you mean

arr0[s] = array[s];

And the function should be declared at least like

uint32_t bin2Int( const uint8_t array[]);

And it is a bad idea to use the magic number 3. You should to add one more parameter that will specify the number of elements in the passed array as for example

uint32_t bin2Int( const uint8_t array[], size_t n );

and use the variable n instead of the magic number 3.

CodePudding user response:

Basically, this function is terrible.

As you use the STMCubeIDE, I assume it is for STM32 uC.

MSB is at index 0:

uint32_t bin24ToInt(const uint8_t *array)
{
    return ((uint32_t)array[0] << 16) | ((uint32_t)array[1] << 8) | array[2];
}

If bits are in the opposite order than usual: (using ARM CMSIS intrinsic).

uint32_t bin24ToIntReverseBitOrder(const uint8_t *array)
{
    return (__RBIT(array[0]) >> 8) | (__RBIT(array[1]) >> 16) | (__RBIT(array[2]) >> 24);
}
  • Related