Skip to main content
SWenn.1
Senior III
January 24, 2023
Solved

C question on pointers???

  • January 24, 2023
  • 4 replies
  • 2430 views

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;
}

    This topic has been closed for replies.
    Best answer by gbm

    My personal favorite for hex conversion is:

    c = "0123456789ABCDEF"[v & 0xf];

    Not always the smallest footprint but cute and compact notation, good for old people.

    :)

    4 replies

    S.Ma
    Principal
    January 24, 2023

    Compile commenting one line at a time to find out what is wrong at compile level.

    At least, you can't return a function local variable. It's like putting a letter on fire in a mailbox...

    I don't use functions returning char *, try passing this as a 2nd function passing parameter. This will avoid your coding issue. Generated code is more efficient if using no more than 2-3 passing parameters, when exceeding, use a pointed structure. Otherwise, check out the generated ASM code to reach this conclusion.

    Return values are mostly error codes.

    And while at it, personnaly, hex conversion could use a simple lookup tables such as :

    const char Quad2Hex[16] = {
    '0', '1',..... '9', 'A', 'B', 'C', 'D', 'E', 'F' };
     
    uint8_t byte = 0xC9;
    char s[10];
     
    s[0] = Quad2Hex[byte >> 4];
    s[1] = Quad2Hex[byte & 0x0F];

    Hope it helps.

    Javier1
    Principal
    January 24, 2023

    >>It's like putting a letter on fire in a mailbox...

    made my day

    hit me up in https://www.linkedin.com/in/javiermuñoz/
    Danish1
    Lead III
    January 24, 2023

    I don’t think it’s a problem with pointers as such, but C strings.

    If you want a C string with 4 characters you must allocate (at least) 5. The wanted characters go into locations 0 to 3 and you have to put an end-of-string marker in location 4. This end-of-string marker has the ascii value 0 i.e. ‘\0’ (not to be confused with the character’0’ which has ascii value 48).

    KnarfB
    Super User
    January 24, 2023

    Any reasonable compiler should give you warnings/errors with the above code which you should understand and not ignore. You may simply use printf and redirect stdout to the UART by re-implementing _write. Alternatively look for (non-standard) itoa function, sprintf, or find and study one of the many hex2ascii implementation on the web.

    hth

    KnarfB

    gbm
    gbmBest answer
    Lead III
    January 24, 2023

    My personal favorite for hex conversion is:

    c = "0123456789ABCDEF"[v & 0xf];

    Not always the smallest footprint but cute and compact notation, good for old people.

    :)

    My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
    Nikita91
    Lead II
    January 24, 2023

    What strange syntax! I'm still amazed at the inventiveness of the programmers...

    gbm
    Lead III
    January 25, 2023

    The code above is NOT an example of strange syntax. Strange syntax is this:

    c = (v & 0xf)["0123456789ABCDEF"];

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