Advertisement
XRahat2011

C++ ClassRoom -- Lecture No. 3

Dec 13th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. OK
  2.  
  3. LESSON: POINTERS
  4.  
  5. Pointers are variables that hold the (memory) address of another variable within them. You can think of them as reference operators but keep in mind that reference operators & pointers are similar but not the same :P.
  6.  
  7. For Example:
  8. int a=5;
  9. int *ptr;
  10. ptr=&a;
  11. the last statement
  12. shows us that the pointer
  13. "ptr"
  14. is a reference to "int a".
  15.  
  16. A pointer is declared
  17. with
  18. *
  19. in front of it
  20.  
  21. Example:
  22. *myFirstPointerEver
  23.  
  24. Some Code:
  25.  
  26. #include <iostream>
  27.  
  28. using namespace std;
  29.  
  30. int main() {
  31.     int a=5;
  32.     int *ptr;
  33.     ptr=&a;
  34.     cout << ptr << endl;
  35. }
  36.  
  37. the first two statements of main()
  38. declares the two variables,
  39. one as "int"
  40. and the other as "integer pointer"
  41. in the third statement
  42. the pointer takes the memory address of the variable
  43. and in the last statement
  44. we print the pointer.
  45.  
  46. That means that the result will show the memory address of int a
  47. usually it is shown as:
  48. 0x.....
  49. lets explain something else,
  50. in the *ptr, the memory address of int a is stored.
  51. So, if we want to print the value of int a.
  52.  
  53. We can do this:
  54.  
  55. #include<iostream>
  56.  
  57. using namespace std;
  58.  
  59. int main() {
  60.     int a=5;
  61.     int *ptr;
  62.     ptr=&a;
  63.     cout << *ptr << endl;
  64. }
  65.  
  66. as you can see
  67. in the last statement
  68. there is a
  69. *
  70. in front of the pointer
  71. & hence it means that this code will print the value of int a (i.e. "5").
  72. Now if we want to change the value of int a itself
  73. (without doing this:
  74. a=75;
  75. // or something else)
  76. we can do it through pointer because it has the memory address to int a & as it has the memory address to int a, we can modify the value of int a very easily without much code.
  77.  
  78. For example:
  79.  
  80. #include<iostream>
  81.  
  82. using namespace std;
  83.  
  84. int main() {
  85.     int a=5;
  86.     int *ptr;
  87.     ptr=&a;
  88.     *ptr=6;
  89.     cout << a << endl;
  90. }
  91.  
  92. as you can see
  93. in the statement
  94. *ptr=6;
  95. we put this
  96. *
  97. before the name of the pointer
  98. because we want to change the value of int a rather than to change the memory address of int a :P.
  99. And in that code, we tell the compiler to change the value of int a to "6".
  100. Now because this pointer has the memory address of int a, automatically the value of int a will become
  101. 6
  102. too.
  103.  
  104. So, in the last statement it prints:
  105. 6
  106.  
  107. Also please look at this interesting code for a moment:
  108.  
  109. #include <iostream>
  110.  
  111. using namespace std;
  112.  
  113. int main() {
  114.     int a=5;
  115.     int *ptr;
  116.     ptr=&a;
  117.     (*ptr)++;
  118.     cout << a << endl;
  119.     // or
  120.     // cout << *ptr << endl;
  121. }
  122.  
  123. this will increment the value of int a by 1
  124. but the interesting thing in it are those "(" and ")"
  125. because if we don't put them, it will not work
  126. because "++" has more power than "*" in C++. (What I mean to say is that C++ executes "++" before "*" & hence those brackets much be used to increment the value of that int a)
  127. So, it means that it will increment the value of int a rather than it's memory address.
  128.  
  129. -- LECTURE ENDED --
  130.  
  131. -- Note --
  132. I will talk about some strings-pointers in the next lesson.
  133.  
  134. ** The End ;)
  135.  
  136. ** Teacher: Hepic
  137. ** Moderator: XRahat2011
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement