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

1 ACCEPTED SOLUTION

Accepted Solutions
gbm
Lead III

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.

🙂

View solution in original post

11 REPLIES 11
S.Ma
Principal

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.

Danish1
Lead II

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).

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

made my day

we dont need to firmware by ourselves, lets talk
KnarfB
Principal III

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
Lead III

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.

🙂

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

Very nice indeed. Intuitive and concise. 👏

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

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

I can't believe it, it works, I tested it on VisuaC.

But I don't understand why...