Public Group

# 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.

## 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 on other sites
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 ssss << 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 on other sites
Quote:
 Original post by Attala101Done :) 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 on other sites
Quote:
 Original post by Attala101Done :) 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 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 :)

A

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 23
• 18
• 13
• 19
• ### Forum Statistics

• Total Topics
634411
• Total Posts
3017289
×