First hallo to everyone, first post here.
Today I found myself wondering if this code is correct, it was written time ago by a friend for a microcontroller and I have no chance to ask him anymore.
The program works correctly, but I can't really decide if it works by luck or not (don't belive in luck with programming). I don't even know if the title of this post is correct, sorry if it could be misleading. The code:
char *textStatusErrorMessage( unsigned int codeStatus )
{
switch ( codeStatus ) {
case STATUS_1:
return ( (char *) " Status: 1 " );
break;
case STATUS_2:
return ( (char *) " Status: 2 " );
break;
default:
sprintf( tmpBuf, " UNKNOWN STATUS %03d ", codeStatus );
return ( (char *) tmpBuf ); //tmpBuf is global
break;
}
}
more precisely this syntax is kind of obscure to me.
return ( (char *) " Status: 1 " );
It returns a char * of what ? where is the "Status: 1 " string saved ? heap/stack???
As it's implemented the string is in the scope of the function and I'm assuming that after leaving the function with the return statement I have no control of what is gonna be written in the pointer returned by the function itself.
From the way I see it I would have had a global array with the different possible string-options and returned a pointer to the correct one selected by the CASE. So I know the pointer I return is to a well defined memory area.
So is this code wrong or not? Which is the most correct solution?
Thanks DAN