Jump to content
  • Advertisement
Sign in to follow this  
Tantere

How to convert int to char * ?

This topic is 4232 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

I'm creating a 2D RPG game with SDL and FMod libraries. I'm using MSVC++ 6.0 I'm trying to display current FPS ingame but I can't convert int to char * Framerate.frate is int Framerate.fps is char * and my function to draw text on screen void DrawString(SDL_Surface *screen, SDLFont *font, int x, int y, char *str,...);
...
Framerate.frate = FRAMES_PER_SECOND;
Framerate.fps = '0' + Framerate.frate;
...
DrawString(Engine.Screen, Engine.menufont, 0, 0, Framerate.fps);
...
I get the following error: error C2664: 'DrawString' : cannot convert parameter 5 from 'char' to 'char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

Share this post


Link to post
Share on other sites
Advertisement
A couple of standard ways to do it:


std::ostringstream ostr;
ostr << frameRate;
std::string frameRateStr(ostr.str());


Or you can use boost


std::string frameRateStr = boost::lexical_cast<std::string>(frameRate);


Share this post


Link to post
Share on other sites
I'm not sure am I doing it right but I tried the first way and get this error:

error C2664: 'DrawString' : cannot convert parameter 5 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Share this post


Link to post
Share on other sites
Once you have the string object, you call it's c_str() function to get the character array (char *)

Share this post


Link to post
Share on other sites
Quote:
Original post by gharen2
Once you have the string object, you call it's c_str() function to get the character array (char *)

c_str() returns a const char*, which cannot be converted to a non-const char* without use of the generally evil const_cast<> operator. If DrawString() is not a library function, however, you can and should convert it to accepting a const char*, or even better, a const std::string&. This will allow the function to be far more flexible, and more technically accurate. (The DrawString() function isn't modifying the string, is it? Then why make it non-const?)

Share this post


Link to post
Share on other sites
You should also be aware that MSVC++ 6.0 is not suitable for C++ development. It's really old and pre-standard, so don't be surprised if the standard library doesn't work like it should. You should download VC2005 instead, the express edition is free.

Share this post


Link to post
Share on other sites
I see what's going on. It seems that you may not understand what pointers are and how they're used.

Let's say that you have this line:

char *fps = '0' + Framerate.frate;

This is not at all what you mean to say. First, if you declare a type to be a pointer, you need to store a *memory address* in the variable. What you are storing here is an *ASCII value*. So, a more appropriate line would be:

char fps = '0' + Framerate.frate;

But, now we have another problem -- you can only store a single digit in a "char" type! So, if 'frate' > 9, then you won't even store a numeral in fps! Hence, we need to find another way to do this. Here is a better way:

*** EDIT DUE TO TOOHRVYK'S CORRECT POST BELOW ***

char fpsString[4];
snprintf(fpsString, "%d", Framerate.frate);

*************************************************


This converts the integer 'frate' into a character string (an array of 'char' types). Since there are four bytes allocated, then storing a 3-digit string with terminator will be no problem! (Note that 'snprintf' is a C function, not a C++ function.)

So now, if you just type 'fpsString', this will evaluate to the address of the first byte in the 'fpsString' array (i.e., it is a char *). Hence, you can without fear write:

DrawString(Engine.Screen, Engine.menufont, 0, 0, Framerate.fps);

[Edited by - AIDev on May 12, 2007 2:40:57 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by AIDev
Here is a better way:

char fpsString[4] = itoa(Framerate.frate);

This converts the integer 'frate' into a character string (an array of 'char' types). Since there are four bytes allocated, then storing a 3-digit string with terminator will be no problem! (Note that 'itoa' is a C function, not a C++ function.)


No.

itoa is neither C nor C++. It's non-standard in both languages, and only provided by some compilers. You are confusing it with atoi, which is standard in C (and carried over in C++) and does the inverse conversion. besides, you are missing two of the three arguments for itoa, and the additional checking for potential overflows. At that rate, you might as well use snprintf.

Also, you cannot assign or initialize a character array from a function call. Character arrays may not be assigned, and may only be initialized from a string literal or an array initializer.

I would definitely write a cleaner version of the string display function (or a wrapper around it) that accepts an std::string. And also, I'd use a standard C++ compiler.

Share this post


Link to post
Share on other sites
Thanks to everybody, this helped me alot. I got it working now like I wanted it to work.

Cheers

Share this post


Link to post
Share on other sites
Seriously though, upgrade your compiler. It's not C++ compliant, period. It's like driving with a smashed car windshield, likely doable but why make your life so much harder? The code generated is a LOT slower, the compiler has known bugs that will never be fixed. No intellisense which is amazing. And 2005 is FREE.

Anyone using VC6 that has a choice (work is forcing it on you) should be arrested and not allowed to program :)

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!