0

I'm getting warnings when I compile something like this...

std::string something = "bacon";

sprintf("I love %s a lot", something.c_str());

Where it says "warning: deprecated conversion from string constant to 'char *'. I tried converting the text to be...

const char *

instead but I get a different error. I'm not committed to sprintf if there is a better option.

11
  • What do you mean by "something like"? The code you posted would error out. Tell us what you're actually doing. Commented Feb 5, 2016 at 17:23
  • 4
    Shouldn't the first parameter of sprintf be a buffer for the result? Commented Feb 5, 2016 at 17:23
  • 1
    "Inserting a string into another string" but you didn't provide "another string" into which to insert. Furthermore, you meant .c_str() not .c_str (which is how I know this isn't your real code). Commented Feb 5, 2016 at 17:24
  • I think you need to call something.c_str() as a function. Commented Feb 5, 2016 at 17:24
  • 3
    Did you check the reference manual first? Commented Feb 5, 2016 at 17:27

3 Answers 3

6

For sprintf to work, you need to provide an array of char big enough to write the result to as the first argument.

However, you can (and should!) just use the far easier operator+ for C++ strings:

std::string res = "I love " + something + " a lot";
Sign up to request clarification or add additional context in comments.

Comments

4
sprintf("I love %s a lot", something.c_str);

In that code, you should call something.c_str() with proper function call () syntax.

Note also that the above use of sprintf() is wrong, since you didn't provide a valid destination string buffer for the resulting formatted string.

Moreover, for security reasons, you should use the safer snprintf() instead of sprintf(). In fact, with snprintf() you can specify the size of the destination buffer, to avoid buffer overruns.

The following compilable code is an example of snprintf() usage:

#include <stdio.h>
#include <string>

int main()
{
    std::string something = "bacon";
    char buf[128];
    snprintf(buf, sizeof(buf), "I love %s a lot", something.c_str());

    printf("%s\n", buf);
}

P.S.
In general, in C++ you may consider string concatenation using std::string::operator+, e.g.:

std::string result = "I love " + something + " a lot";

3 Comments

Thank you. I'll have to give snprintf a look as well. Visual Studio did complain about sprintf being not safe to use and to try sprintf_s instead. I'll have a look at snprintf as well.
snprintf() is standard; sprintf_s() is Microsoft-specific. I think snprintf() has been implemented in Visual C++ since VS2015.
I ended up having to go with snprintf. For some reason, I cannot use operator+ with the substr method on older versions of GLIBCXX which I don't have any control over. While it compiles, running the app fails with the message saying I need a newer version of GLIBCXX. Thanks for giving this alternative
-4

It doesn't look like a correct use of sprintf. First parameter is supposed to be a char * with already a backing memory. For example:

char *str = malloc (BUFSIZ);
sprintf (str, "I love %s a lot", something.c_str);

4 Comments

Also c_str() needs to be a function call.
If your challenge is something from C++ point of view I don't accept the challenge. I just gave hint about correct general usage of sprintf.
Thanks guys. I did not use sprintf properly to be begin with.
Don't use malloc in C++. Use new unless you absolutely need to use malloc(normally interfacing with a C api).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.