fork download
  1. // If you are not sure what some lines of code do, try looking back at
  2. // previous example programs, notes, or ask a question.
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. // This is totally unrestricted - we can edit the pointed-to data and reassign 'value,'
  9. // which will actually change where the passed-in pointer is pointing to at the call site.
  10. void func1(char*& value);
  11.  
  12. // Here, we can still edit the pointed-to data and reassign 'value,' but this cannot
  13. // effect its caller.
  14. void func2(char* value);
  15.  
  16. // Here, we can reassign value within func3, but we cannot edit the pointed-to data.
  17. void func3(const char* value);
  18.  
  19. // Here, we can edit the pointed-to data, but we cannot reassign 'value' within func4.
  20. void func4(char* const value);
  21.  
  22. // Finally, we can neither reassign 'value' nor edit its data.
  23. void func5(const char* const value);
  24.  
  25.  
  26. int main() {
  27.  
  28. char cstring[50];
  29. int integer = 0;
  30.  
  31. // Creates an integer pointer and assigns it to the address of "integer"
  32. int* valuePtr = &integer;
  33.  
  34. cout << valuePtr << endl; // This outputs the address of "integer"
  35. cout << *valuePtr << endl; // This will output the actual value of "integer"
  36. *valuePtr = 5; // This actually sets the value in "integer" to 5
  37. cout << integer << endl; // This will output the 5
  38.  
  39.  
  40. // Creates a pointer to a character and points it to the first element in the c-string array
  41. char* stringPtr = cstring;
  42. cout << "Enter a string: ";
  43. cin >> cstring;
  44.  
  45. // Iterate through the string using the pointer
  46. while(*stringPtr) {
  47. cout << *stringPtr << endl;
  48. stringPtr++;
  49. }
  50. // Now, "stringPtr" will be pointing off the end of the array, so using it would likely result in a seg fault.
  51. // It's a good idea to set it to NULL so that you program can check if it is valid in the future.
  52. stringPtr = NULL;
  53.  
  54.  
  55. // Declares a void pointer and sets it to point to "integer"
  56. void* voidPtr = &integer;
  57. *(int*)voidPtr = 10; // Sets the value to 10 - note that you must specify that "voidPtr" is pointing to an integer
  58. cout << endl << *(int*)voidPtr << endl; // Again, you must specify that "voidPtr" is pointing to an integer
  59.  
  60.  
  61. // End program
  62. cout << endl;
  63. system("pause");
  64. return 0;
  65. }
  66.  
  67.  
  68. // See Prototypes
  69.  
  70. void func1(char*& value) {
  71. value = "a";
  72. // It may not make sense why we might want to change the pointer in main
  73. // right now, but it will when we talk about dynamic memory next week.
  74. }
  75.  
  76. void func2(char* value) {
  77.  
  78. // Sets the values in the c-string pointed to by value
  79. for(int i = 0; *value; i++) {
  80. *value = 'a' + i;
  81. value++;
  82. }
  83. value = NULL;
  84. }
  85.  
  86. void func3(const char* value) {
  87.  
  88. // Can't edit the pointed-to values
  89.  
  90. value = NULL;
  91. }
  92.  
  93. void func4(char* const value) {
  94.  
  95. // Sets the values in the c-string pointed to by value
  96. for(int i = 0; *value; i++) {
  97. value[i] = 'a' + i;
  98. }
  99.  
  100. // value = NULL; can't do this
  101. }
  102.  
  103. void func5(const char* const value) {
  104.  
  105. // Can't edit anything
  106. }
  107.  
Success #stdin #stdout #stderr 0s 15240KB
stdin
hello!
stdout
0x7ffd7cc29c0c
0
5
Enter a string: h
e
l
l
o
!

10

stderr
sh: 1: pause: not found