[ create a new paste ] login | about

Link: http://codepad.org/8Ktx3GZ3    [ raw code | output | fork ]

za3k - C++, pasted on Jul 2:
#include <iostream>
#include <string>
char* charstarcopy(const std::string s)
{
    const char* a = s.c_str();
    int l = strlen(a);
    char* b = new char[l+1];
    strcpy(b, a);
    return b;
}

using namespace std;
int main()
{
  string a("aaa");
  char* b = charstarcopy(a);
  char* c = charstarcopy(b);
  a[0] = 'b';
  b[1] = 'b';
  c[2] = 'b';
  cout << a << endl;
  cout << b << endl;
  cout << c << endl;
  delete[] b;
  cout << a << endl;
  cout << c << endl;
  delete[] c;
  return 0;
}


Output:
1
2
3
4
5
baa
aba
aab
baa
aab


Create a new paste based on this one


Comments: