1

I'm hoping that this will be a pretty basic problem that is maybe due to my lack of understanding a lot of the C++ Win32 API utlities.

Anyway, I'm having some trouble with the sprintf() function. I had no problem using it with a string like this:

//memory is a void pointer that maps a file to shared memory sprintf((char *)memory, "Shared memory message"); //Write to shared memory 

But when I try to use a string variable it doesn't work....

sprintf((char *)memory, str_var); //Write to shared memory 

I get an error that says: no suitable conversion function from "std::string" to "const char *" exists. I am not even able to fix this by type casting, like I did with memory.

This seems pretty inconsistent. What am I doing wrong, and how do I give it a value that it will accept?

2

1 Answer 1

2

Iff you need sprintf, you need to pass a const char*:

So e.g.

sprintf((char *)memory, str_var.c_str()); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.