Advertisement
bhok

Week 6

Jan 16th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. //----------------------------------
  2. // Week 6 : Pointers
  3. //----------------------------------
  4.  
  5. // I remembered when I was studying object-oriented at the university level and
  6. // had visited the student tutor.
  7. // I had asked him to explain what is a pointer and can you explain it to me?
  8. // The only thing I recalled from that particular lesson was
  9. // "follow the pointer". Yeah...That wasn't really that helpful.
  10. // To be honest, it took me a while to figure out what a pointer truly meant.
  11. // Even today, I think my grasp of pointers is pretty good,
  12. // but my mastery of advanced level of pointer still needs work.
  13. // Let's start with the fundamentals first.
  14.  
  15. // What is a pointer?
  16. // The best metaphor that I came up with is, a pointer is a finger and nothing more.
  17. // A finger can point at things and tell you what's at that specific location.
  18. // When it comes to development, a pointer is meant to "point" at a specific address and
  19. // tell you what's at that address.
  20. // Is it an integer, is it a character, or is it a string? Who knows.
  21.  
  22. // What is the purpose of a pointer?
  23. // To conserve memory and make efficient programs.
  24. // Instead of working with actual data, wouldn't it be
  25. // easier just to point at what you need instead?
  26.  
  27. // What I didn't know until recently is that a pointer can only point to three things.
  28. // A pointer can only point at another address, another pointer, or a null value.
  29. // You can't have a pointer point at something else
  30. // besides these three.
  31.  
  32. // So, let's start.
  33. // Declaring a pointer
  34. // There are three ways to declare a pointer, but they all mean the same thing.
  35. // int* pointer1;
  36. // int * pointer1;
  37. // int *pointer1;
  38. // I generally like the first declaration of the pointer.
  39. // What you see here is a "*" symbol, correct?
  40. // That just means that you have declared a pointer that is pointing to an int dataspace.
  41. // So, you must be dealing with integer only.
  42.  
  43. // Intializing a pointer
  44. // int* pointer1 = 5;
  45. // pointer1 is pointing to the value of 5.
  46. // pointer1 is not the value of 5, but rather just points to it.
  47.  
  48. // std::cout << pointer1;
  49. // When we perform this, we can see that pointer1 is the address of the value 5.
  50. // std::cout << *pointer1;
  51. // When we dereference pointer1 with *, we can now go to the address of pointer1 and
  52. // see that there is a value of 5 at that address.
  53.  
  54. // Exercises
  55. //
  56.  
  57. // How to move a pointer
  58.  
  59. // Now, we see that pointers can be used to point at a single object.
  60. // How about we see how pointers are used on a larger scale.
  61.  
  62. int main()
  63. {
  64. // declare out first array
  65. int array[5] = {0,1,3,4,5};
  66. // declare our pointer and have intialize it to our array.
  67. // pointer1 is now equals to array.
  68. int* pointer1 = array;
  69.  
  70. // This should output the first element of the array
  71. std::cout << *pointer1;
  72.  
  73. // Now can you guess what these numbers are?
  74. std::cout << *(pointer1 + 2);
  75. std::cout << pointer1[3];
  76. }
  77.  
  78. // Exercises Math Pointers****
  79.  
  80. // How to use a pointer in a function
  81.  
  82.  
  83. // Pointer Math Exercises
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement