Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Garbage when using for loop with fonts

Credits go to Chris Katko for helping out!
This thread is locked; no one can reply to it. rss feed Print
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"}611925 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.
"Love thy neighbor as much as you love yourself means be nice to the people next door. Everyone else can go to hell. Missy Cooper.
The advantage to learning something on your own is that there is no one there to tell you something can't be done.

Chris Katko
Member #1,881
January 2002
avatar

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:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

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.
"Love thy neighbor as much as you love yourself means be nice to the people next door. Everyone else can go to hell. Missy Cooper.
The advantage to learning something on your own is that there is no one there to tell you something can't be done.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

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.

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
avatar

That's horrifying. O_O

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

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
avatar

Peter Hull
Member #1,136
March 2001

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"}611931

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Peter Hull, I loved that you answered my rhetorical question. You're my hero. ;D

{"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"}611932

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.

Go to: