code_junkie

Two basic C++ questions about string literals and dynamic allocation

Nov 13th, 2011
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. char * aString = "This is a string.";
  2.  
  3. char aString[] = "This is a string.";
  4.  
  5. int someNumber;
  6.  
  7. int * someNumber = new int;
  8.  
  9. char *aString = "This is a string.";
  10.  
  11. char aString[] = "This is a string.";
  12.  
  13. int someNumber;
  14.  
  15. int *someNumber = new int;
  16.  
  17. void func() {
  18. char foo[] = "hello";
  19.  
  20. cout << foo << endl;
  21.  
  22. foo[2] = 'w';
  23. foo[3] = 'w';
  24.  
  25. cout << foo << endl;
  26. }
  27.  
  28. func();
  29. func();
  30.  
  31. hello
  32. hewwo
  33. hello
  34. hewwo
  35.  
  36. void func() {
  37. const char* c_foo = "hello";
  38. char* foo = const_cast<char*>(c_foo);
  39.  
  40. cout << foo << endl;
  41.  
  42. foo[2] = 'w';
  43. foo[3] = 'w';
  44.  
  45. cout << foo << endl;
  46. }
  47.  
  48. hello
  49. hewwo
  50. hewwo
  51. hewwo
Add Comment
Please, Sign In to add comment