I have a file which has a static constant string and a function which will return pointer to that string. File looks like this:
typedef unsigned char BOOLEAN;
#define TRUE 1
#define FALSE 0
static const unsigned char MAL_Version[8] = "2.001";
/* Function to return Version string */
BOOLEAN GetVersion ( unsigned char* pu8Version )
{
BOOLEAN success = FALSE;
if(pu8Version != NULL)
{
pu8Version = &MAL_Version[0];
success = TRUE;
printf("\r\nTRUE");
printf("\r\n%s", pu8Version);
}
return success;
}
and in main(), I declare an array and pass it's address to GetVersion function. When I do this, I am getting random characters.
int main() {
unsigned char buffer[10];
GetVersion(buffer);
printf("\r\n%s", buffer);
}
Output is:
TRUE
2.001
D�3�
What I am missing? The pointer in function is correctly printing the string, but when it returns, it prints garbage.
typedef unsigned char BOOLEAN;, instead oftypedef int BOOLEAN;?intgenerally has better performance than the other types, because it's the natural word size of the machine, and therefore often the most performant.