cancel
Showing results for 
Search instead for 
Did you mean: 

C question on pointers???

SWenn.1
Senior III

Hello all....

Ok....not sure what i am missing here but this fails miserably....

The compiler hates when I try and assign test = 

The return function works fine, I have made the return variable static so as not to lose the scope....test is an array so I expect test is the same as me doing &test[0]...What am I missing here?

uint8_t test[6];
 
  for (uint8_t k = 0; k < NO_ADC_CH; k++)
  {
	  test = hex2Ascii(&stream[k]);
	  test[4] = '\r';
	  test[5] = '\n'; //new line
	  HAL_UART_Transmit_IT(&huart2, test, sizeof(test));
  }
 
 
char*hex2Ascii(uint16_t *val)
{
    unsigned char d;
    static char buff[4];
 
    buff[0] = (char)((d = (*val >> 12)) > 9 ? ('A' + d - 10) : ('0' + d));
    buff[1] = (char)((d = ((*val >> 8) & 0xf)) > 9 ? ('A' + d - 10) : ('0' + d));
    buff[2] = (char)((d = ((*val >> 4) & 0xf)) > 9 ? ('A' + d - 10) : ('0' + d));
    buff[3] = (char)((d = (*val & 0xf)) > 9 ? ('A' + d - 10) : ('0' + d));
    return buff;
}

11 REPLIES 11

By definition a[b] is the same as *(a + b), which is the same as *(b + a), which is the same as b[a]. That's just C.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

Live and learn, as they say ))