2
std::string mystring;

sprintf(mystring.c_str(), "%s %d", "Helloworld", 2014);

Its is giving a compiler error to me:

'sprintf' : cannot convert parameter 1 from 'const char *' to 'char *'

drescherjm
  • 10,365
  • 5
  • 44
  • 64
user3706458
  • 51
  • 1
  • 5
  • 1
    first parameter of sprintf should be a char * but you are passing const char *. **mystring.c_str()** is const char *. you can use append to do whatever you want to do with sprintf **mystring.append("Helloworld 2014");** – Gangadhar Sep 25 '14 at 08:02
  • First argument of "sprintf" is a pointer to character array 'char *' where the formatted string will be saved. In the other hand, mystring.c_str() is a contant character array which is generated from mystring. Data type of them is different. – Fumu 7 Sep 25 '14 at 08:03

6 Answers6

10

It shouldn't be a warning, it should be an error. The pointer returned by std::string::c_str() points to read-only memory; any attempt to write through it is undefined behavior. (In your case, if you use a const_cast to shut up the compiler, you're code will probably crash, since you're calling c_str() on an empty strying.)

Generally speaking, what you probably want is std::ostringstream:

std::ostringstream formatter;
formatter << "Helloworld" << ' ' << 2014;
std::string myString = formatter.str();

FWIW: sprintf is one of the most dangerous functions in the standard library, and only present for historical reasons. It's almost impossible to use safely; even in C, you should prefer snprintf (but in C++, std::ostringstream is far better).

James Kanze
  • 150,581
  • 18
  • 184
  • 329
9

your warning gives you all information you need.

std::string::c_str() returns a const char* and sprintf requires a char* since it modiefies the buffer.

But you are writing c++ and should avoid sprintf. Use a ostringstream to write formated data to a string.

vlad_tepesch
  • 6,681
  • 1
  • 38
  • 80
  • It's `std::string::c_str()`. And he should use `std::ostringstream` for writing, not `std::stringstream`. – James Kanze Sep 25 '14 at 08:02
  • @JamesKanze yes i corrected the typo. With 'stringstream' i meant more the concept than the specific class. Otherwise i had used backticks - but i will add too. – vlad_tepesch Sep 25 '14 at 08:04
2

std::string manages underlying C-style buffer. c_str returns const char* because it shouldn't be modified by anything other then string's methods.

You should rather use ostringstream. See this question: C++ equivalent of sprintf?

Community
  • 1
  • 1
Tomo
  • 3,431
  • 5
  • 25
  • 28
2

You are telling sprintf to store the result in mystring.c_str(). This is a readonly view of the underlying representation of mystring. Since it is readonly (or const char *), you can't write the result to it.

If you need to use sprintf, you will have to create a writable character buffer for it to use, and then assign that buffer to mystring.

A different way of performing this sort of operation without having to create character buffers and deal with possible overflow would be to use a stringstream:

std::stringstream buffer;
buffer << "Helloworld " << 2014;
mystring = buffer.str();
Gnagn
  • 151
  • 6
0

Alternatively, use old C snprintf into a temporary buffer, then assign into the string :

 std::string mystring;
 char buf[64];
 snprintf(buf, sizeof(buf), "%s %d", "Helloworld", 2014);
 mystring.assign(buf);

snprintf is always safer than sprintf since it avoids buffer overflow.

But of course using an ostringstream like answered here by James Kanze is better.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0
#include <iostream>
#include <string>
#include <sstream>  

 std::stringstream ss;
 ss << "Helloworld";
 ss << " ";
 ss << "2014";
 ss << std::endl;
 std::string str = ss.str();
 std::cout << str;

 const char * mystring= str.c_str();

In this way also you can append int or long number into the string.

Example:

 long year = 2014;

 std::stringstream ss;
 ss << "Helloworld";
 ss << " ";
 ss << year;
 ss << std::endl;
 std::string str = ss.str();
 std::cout << str;

 const char * mystring= str.c_str();
borchvm
  • 3,533
  • 16
  • 44
  • 45