Advertisement
bhok

4.1 Pointers Beginning

Jul 13th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. // More tutorials at BrandonHok.com
  2.  
  3. #include <iostream>
  4.  
  5. // C++ Crackdown 4.1 - Pointer Beginnings
  6.  
  7. // What are pointers you may ask?
  8. // A pointer in its simplest term is just
  9. // an arrow that points to a specific memory address
  10. // -> 1008 Fire Storm Miami
  11.  
  12. // What is the purpose of a pointer?
  13. // To save memory space and for large variables.
  14.  
  15. // Metaphor Example
  16. // If I live on 1008 Fire Storm Miami
  17. // Then the pointer is literally the ->
  18. // Or sign that says [To The Right Is 1008 Fire Storm Miami]
  19. // A sign just points to an address, but the actual sign is not the house.
  20. // A sign just "points" to the specific desired address.
  21.  
  22. // Metaphor Example
  23. // We are at a parking lot together.
  24. // I ask you to tell me where is the red car.
  25. // You point with your finger at the red car.
  26. // Obviously the answer is not your finger, but I can look
  27. // at your finger and it will point me towards the answer.
  28. // I can ask you to use your other fingers to point me at different
  29. // color cars (where other data points are found).
  30.  
  31.  
  32. int main()
  33.  
  34. {
  35.     // Section 1
  36.     // Type the following code below until Section 2.
  37.  
  38.     // Declare our first variable
  39.     int y[5] = { 10,2,3,4,5 };
  40.     // declare our pointer and intitalize it to the address(&) of y
  41.     int *x = &y[0];
  42.  
  43.     // Take a look at the numbers and check it out
  44.     // How about changing *x to &x, you'll see something special like a computer address
  45.     // Surprise!
  46.     std::cout << y << std::endl;
  47.     system("pause");
  48.    
  49.     // Section 2
  50.     // Now you may type in the rest of the code =)
  51.     // Look carefully and see what are the differences between thw two
  52.     std::cout << x << std::endl;
  53.     std::cout <<*x;
  54.     std::cout << *(x + 3);
  55.     system("pause");
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement