Wednesday, 28 August 2013

Printing output from string building function not returning expected result

Printing output from string building function not returning expected result

I'm writing a function to output a basic hours & minutes string in 24 hour
format from two global int's containing hours and minutes.
I've defined these during initialization:
int g_alarmHours = 7;
int g_alarmMinutes = 0;
The function to return the string is:
char* getAlarmTime() {
int hours = g_alarmHours;
int minutes = g_alarmMinutes;
char t[6];
t[0] = (hours/10) + '0';
t[1] = (hours%10) + '0';
t[2] = ':';
t[3] = (minutes/10) + '0';
t[4] = (minutes%10) + '0';
t[5] = 0;
return t;
}
The global variables are stubs to be replaced when serial comms to another
device are added where those values will be retrieved from.
Calling the function generates the following hex values at the character
pointer:
0x20 0x4b 0x00
When I replace the top two lines of the getAlarmTime() function with the
following
int hours = 7;
int minutes = 0;
The output is then what I expect, of:
07:00\0
Why is using those global variables causing the output of getAlarmTime()
to go so wonky?

No comments:

Post a Comment