When I look in the debugger watch, with breakpoint at _render
, command
containins only 0x00
.
This is for a AVR processor.
int main(){
char hello[] = "Hello";
gfx_put_string(5, hello);
}
void gfx_put_string(int length, char* characters){
char command[length+3];
command[0] = 0x00; //cmd
command[1] = 0x06; //cmd
for (int i=0; i<length; i++){
command[i+2] = characters[i];
}
command[length+2] = 0x00; //end with null
_render(length+3, command);
}
Want it to contain 0x00, 0x06, H, e, l, l, o, 0x00
. What is wrong with my code? How can I fix it?
Edit: Suggested fix was to change the first character in the array not being 0x00 (null), but no change. Optmization is turned of. XC8 None (-O0)
So the problem was something with the command
array being variable length, when I set the length to a const everything worked out as expected. Final code below:
void gfx_put_string(const int length, char* characters){
char command[length+3];
command[0] = 0x00;
command[1] = 0x06; //cmd
for (int i=0; i<length; i++){
command[i+2] = characters[i];
}
command[length+2] = 0x00; //end with null
_render(length+3, command);
}