Hi
snprintf is working fine when i am not using append function . But as soon as i use append function snprintf crashes . Please suggest
why snprintf is crashing after append . What is special with append function which is crashing this program
Below is the program.
#include<string>
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char *s;
string name="ESACPE ";
name.append("hello '\\' ");
cout<<snprintf(s,30,name.c_str());
getch();
return 0;
}

After that think again. It seems you have wrote absolutely senseless code with this awkward jumble of std::string and C-string oriented snprintf ;)...

hi
I know that it is awkward that i am jumbling C++ string snprintf But my point here is that this same code is working fine when i am not calling append function of std::String and it crashes when i try append function . Why code is crashing only after call of append and snprintf ?

As opposed to say crashing when you do this?

char *s;
string name="ESACPE ";
cout<<snprintf(s,30,name.c_str());

Who knows, dumb luck perhaps.
It's certainly got the potential to crash at any moment.

> name.append("hello '\\' ");
The fact that this made it crash does NOT imply that your previous attempt was bug-free.


Try say
char s[100]; // was char *s

This question has already been answered. Start a new discussion instead.