|
Garbage when using for loop with fonts |
Bob Keane
Member #7,342
June 2006
|
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? By reading this sig, I, the reader, agree to render my soul to Bob Keane. I, the reader, understand this is a legally binding contract and freely render my soul. |
Chris Katko
Member #1,881
January 2002
|
I think you're sending a [character] to a [string argument] without a string terminator. Bob Keane said: &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. -----sig: |
Bob Keane
Member #7,342
June 2006
|
Chris Katko with the win. And it appears the address of operand is optional. Thanks. By reading this sig, I, the reader, agree to render my soul to Bob Keane. I, the reader, understand this is a legally binding contract and freely render my soul. |
Edgar Reynaldo
Major Reynaldo
May 2007
|
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. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Peter Hull
Member #1,136
March 2001
|
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)
|
Chris Katko
Member #1,881
January 2002
|
That's horrifying. O_O -----sig: |
raynebc
Member #11,908
May 2010
|
I've never run into the scenario, but would certain platforms malfunction with that code due to different endianness? |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Try it on big endian and let us know. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Peter Hull
Member #1,136
March 2001
|
Edgar Reynaldo said: 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)
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
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"} My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Peter Hull
Member #1,136
March 2001
|
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.
|
|