Jump to content
  • Advertisement
Sign in to follow this  
deadlydog

How to pass a char as a char*

This topic is 4647 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, basically I'm working on the entering high scores section of my game. Now I want to display the entire alphabit to the user so they can use the mouse to click on them. Here's my code I'm using to display the characters I was visible:
// Loop through all letters and display them
// NOTE: 32 is a Space so don't display it normally
// NOTE: 127 is DEL so don't display it normally
// NOTE: 8 is Backspace
for (iIndex = 33; iIndex < 127; iIndex++)
{
   cLetter = CHAR(iIndex);
   cpLetter = &cLetter;
   cpApp->mcFontRegular.Print(cpLetter, 50 + ((iIndex % iNumberOfLettersPerLine) * 20), 200 + ((iIndex / iNumberOfLettersPerLine) * 30), 0, 0, sDisplayedLettersTextColor);
}


Now, don't worry about the code after the cpLetter parameter of the Print function. Alright, the Print function takes a char* as it's first parameter, so what I'm doing is I'm using iIndex as the ASCII value of the letters, so a char is returned through CHAR(iIndex) to cLetter. I then just create a simple pointer to the cLetter (cpLetter) to pass in the Print function. This works, except that it prints the letter plus some other characters, sorta looks like "iiiiii". Now, even when I debug I see these extra characters. I've been working on this for over an hour trying all sorts of different things, such as using character arrays, actually allocating cpLetter some memory and using it, making cpLetter[1] = '\n' (cpLetter[0] would equal the letter), type casting, etc. I just can't seem to get it to work. I always get those darn extra characters. Anyone know how I can do this or have any suggestions? Thanks

Share this post


Link to post
Share on other sites
Advertisement
It is not a NUL terminated string, so it is probably continuing to read until it finds a '0'.

Try this:



// Loop through all letters and display them
// NOTE: 32 is a Space so don't display it normally
// NOTE: 127 is DEL so don't display it normally
// NOTE: 8 is Backspace



// Here we declare a character buffer of a size 2, 1 space
// for the character, and one for the '\0' terminator.
char cLetter[2];
cLetter[1] = '\0'; // Set the NUL terminator

for (iIndex = 33; iIndex < 127; iIndex++)
{
cLetter[0] = CHAR(iIndex); // Set the letter
//cpLetter = &cLetter; WE HAVE NO NEED FOR A VARIABLE cpLetter
cpApp->mcFontRegular.Print(&cLetter[0], 50 + ((iIndex % iNumberOfLettersPerLine) * 20), 200 + ((iIndex / iNumberOfLettersPerLine) * 30), 0, 0, sDisplayedLettersTextColor);
}





Share this post


Link to post
Share on other sites
It looks like IIII because there is no null terminator after the character and it will print until it finds one. What you want to do is create a string and make it hold just the one letter with a NULL terminator, like this.

cLetter = CHAR(iIndex);

char str[2];

str[0] = cLetter;

str[1] = '\0'; // NULL terminator

Hope that helps.

ace

Share this post


Link to post
Share on other sites
Oh my god, I knew that. For some reason I was thinking that '\n' was the null line terminator, not '\0'. Ahhhhh, I'm an idoit. I tried that right away too using '\n'. There goes an hour of my time for no reason. Thanks a lot though guys.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!