returning a c_str() not working.

1
2
3
4
5
6
7
8
9
10
11
char *strswap(const string & s)
{
    const char * temp = new (nothrow) char[s.size()];
    temp = s.c_str();
    char * rv = new (nothrow) char [s.size()];
    int i;
    for (i = 0; i < s.size(); ++i)
        rv[i] = temp[i];
    rv[i] = 0;
    return rv;
}

(also, where should I delete temp and rv?)

1
2
3
4
5
6
7
8
9
10
11

string str; char ch; char * chp; char charray[16];

int main()
{
     chp = new (nothrow) char [16]; 
     getline(cin, str);
     chp = swap(str);
     cout << "chp = " << chp << endl;
     return 789;
}


It compiles and runs I get:
chp =
Topic archived. No new replies allowed.