fork download
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. void foo(char* a, char* b){
  5. char* temp = new char[strlen(a)+1];
  6.  
  7. strcpy(temp, a);
  8. std::cout << "temp = " << temp << " a = " << a << " b = " << b << std::endl;
  9.  
  10. strcpy(a, b);
  11. std::cout << "temp = " << temp << " a = " << a << " b = " << b << std::endl;
  12.  
  13. strcpy(b, temp); // this copies 6 bytes to b, which puts a 0 in the first byte of a.
  14. std::cout << "temp = " << temp << " a = " << a << " b = " << b << std::endl;
  15.  
  16. delete[] temp;
  17. }
  18.  
  19. int main() {
  20. char a[] = "First";
  21. char b[] = "Last";
  22.  
  23. std::cout << "a size is " << sizeof(a) << std::endl;
  24. std::cout << "b size is " << sizeof(b) << std::endl;
  25.  
  26. std::cout << "address of a[0] is " << (void*)&a[0] << std::endl;
  27. std::cout << "address of b[0] is " << (void*)&b[0] << std::endl;
  28.  
  29. foo(a, b);
  30.  
  31. std::cout << "A After: "<< a << "\n";
  32. std::cout << "B After: "<< b << "\n\n";
  33. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
a size is 6
b size is 5
address of a[0] is 0xbfe1ffaa
address of b[0] is 0xbfe1ffa5
temp = First a = First b = Last
temp = First a = Last b = Last
temp = First a =  b = First
A After: 
B After: First