Jump to content
  • Advertisement
Sign in to follow this  
Attala101

Total char * confusion

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

Done :) Making good progress here :)
Now I have a problem with my sprintf and printf functions though.

sprintf_s(MyText->strBuffer.c_str(),"%s is making it's turn", nations[movingnation]->sCountryName);
MyText->newtext = MyText->strBuffer;

This was working fine when I was working with char arrays. Now it gives me the error:

error C2664: 'sprintf_s' : cannot convert parameter 1 from 'const char *' to 'char *'

The sprintf function is not my own, (big surprise), so how do I convert from const char * to char * ?

Share this post


Link to post
Share on other sites
Advertisement
I don't know if you know it, but const means that a type can't be changed (read-only). When you use std::string you can't directly manipulate the char* since it might be stored in another way, or use optimizations which wouldn't be possible if the char* was non-const. Your problem is that you still use the CRT (C Runtime) functions instead of the C++ Standard Library's functions.

sprintf_s(MyText->strBuffer.c_str(),"%s is making it's turn", nations[movingnation]->sCountryName);
MyText->newtext = MyText->strBuffer;


Could be written like this (you need to include <sstream> before using string streams):


std::stringstream ss
ss << nations[movingnation]->sCountryName << " is making it's turn";
MyText->strBuffer = ss.str();
MyText->newtext = MyText->strBuffer;

I don't see why you use both strBuffer and newtext, but this code does the same as your code should do in the above example.

Streams is a powerful utility in the C++ Standard Library, you are most likely familar with cout which is a stream (output). Or cin which is also a stream (input).

EDIT: As you can see below in rip-off's post there are many ways to do this. In this case simply appending strings (like rip-off does) might be a better choice. But if you want to append stuff like ints, floats and custom types, or if you want to be able to specify which base (hexidecimal, binary, decimal) numbers should be printed in, if floats should be printed in scientific format etc. then you should use streams.

Share this post


Link to post
Share on other sites
Quote:
Original post by Attala101
Done :) Making good progress here :)
Now I have a problem with my sprintf and printf functions though.

sprintf_s(MyText->strBuffer.c_str(),"%s is making it's turn", nations[movingnation]->sCountryName);
MyText->newtext = MyText->strBuffer;

This was working fine when I was working with char arrays. Now it gives me the error:

error C2664: 'sprintf_s' : cannot convert parameter 1 from 'const char *' to 'char *'

The sprintf function is not my own, (big surprise), so how do I convert from const char * to char * ?
The first argument to sprintf_s() is the target buffer, so it has to be non-constant (mutable). c_str() returns a constant pointer; it's not meant to provide mutable access to the contents of std::string.

This is a sign of sorts that you're mixing C and C++ in 'unnatural' ways ;-) The first step in switching to the C++ way was replacing raw character pointers with std::string. The next step will be to ditch sprintf_s() and its kin in favor of streaming (or a safer substitute such as boost::format). Ask if you need more details.

[Edit: Bah, too slow.]

Share this post


Link to post
Share on other sites
Quote:
Original post by Attala101
Done :) Making good progress here :)
Now I have a problem with my sprintf and printf functions though.

sprintf_s(MyText->strBuffer.c_str(),"%s is making it's turn", nations[movingnation]->sCountryName);
MyText->newtext = MyText->strBuffer;

This was working fine when I was working with char arrays. Now it gives me the error:

error C2664: 'sprintf_s' : cannot convert parameter 1 from 'const char *' to 'char *'

The sprintf function is not my own, (big surprise), so how do I convert from const char * to char * ?


Ugh. Try:


std::string thingToBePrinted = nations[movingnation]->sCountryName;
thingToBePrinted += " is making it's turn";

printFunctionThatTakesCharPointer( thingToBePrinted.c_str() );



Share this post


Link to post
Share on other sites
I'm not sure I understood everything here, but I'm gonna read up on this. I actually thought I was using standard C++ functions, so I have learned a lot today.

Oh, and the program compiles fine now :)

Thanks for your help!

A

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!