I am trying to use a for loop to label rows and columns in a program. here is the code:
for(char ch = 'A'; ch < 'K'; ch++){ al_draw_text(font, white, x_position, y_position, ALLEGRO_ALIGN_CENTRE, &ch); x_position = x_position + column_width;}
but getting garbage.
{"name":"611925","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/5\/05238f0b4b62970c63cbfd5e29795173.png","w":1366,"h":768,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/5\/05238f0b4b62970c63cbfd5e29795173"} I tried three fonts but get the same results. Any ideas?
I think you're sending a [character] to a [string argument] without a string terminator.
&ch);
is 'A' (etc) but needs to be 'A\0' (null terminator) for string parsing functions to know when to stop parsing the character array.
You could either use sprintf or:
//UNTESTED. Using a 2 letter char array folded into the loop. char char_str[2]; //hold letter + '\0' char_str[1] = '\0'; for( char_str[0] = 'A'; char_str[0] < 'K'; char_str[0]++){ al_draw_text(font, white, x_position, y_position, ALLEGRO_ALIGN_CENTRE, &char_str); //can't remember if you need & on char_str or not x_position = x_position + column_width;}
(I haven't done C in forever.)
[edit]
https://onlinegdb.com/SJ8C0wXLE
Code output: ABCDEFGHIJ
Looks good to me with printf.
Use al_draw_textf and %c
Chris Katko with the win. And it appears the address of operand is optional. Thanks.
Except you don't need a char* when you use %c and printf. :/
Your code draws garbage because your char is allocated on the stack. The next three characters happenend to be the two displayed and a null. Just depends on what is in memory at the time.
Actually you can
int main () { int c; for (c = 'A'; c < 'K'; ++c) { printf ("%s", &c); } return 0; }
but you will be reincarnated as a cockroach. (reason: as long as you're on a little-endian system, the int 'A' will be stored in memory as 0x41000000 and the null terminator will be there as the second byte)
That's horrifying. O_O
I've never run into the scenario, but would certain platforms malfunction with that code due to different endianness?
Try it on big endian and let us know.
Try it on big endian and let us know.
I dusted off (literally) my iBook G3 which has a big-endian PowerPC. Predictably, nothing get printed. (reason: the int 'A' will be stored in memory as 0x00000041 and the null terminator will be there as the first byte)
{"name":"611931","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/5\/05b2dbe19010dbc26110e6cc2397bbcf.jpg","w":2448,"h":3264,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/5\/05b2dbe19010dbc26110e6cc2397bbcf"}
Peter Hull, I loved that you answered my rhetorical question. You're my hero.
{"name":"611932","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/2\/42cd0e9b126f3f8a322116528a567100.png","w":949,"h":639,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/2\/42cd0e9b126f3f8a322116528a567100"}
I'd got as far as downloading, building and installing an emulator for the Dragon 64 computer which I know had a big endian 6809 processor but I couldn't figure out how to mount disk images to get a C compiler on it. Then I remembered the old faithful Mac upstairs. I did quite a lot of Allegro-ing on it back in the day; as well as Alex, you can see icons for Overgod and KQ which I did Mac builds for.