Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- OK
- LESSON: POINTERS
- 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.
- For Example:
- int a=5;
- int *ptr;
- ptr=&a;
- the last statement
- shows us that the pointer
- "ptr"
- is a reference to "int a".
- A pointer is declared
- with
- *
- in front of it
- Example:
- *myFirstPointerEver
- Some Code:
- #include <iostream>
- using namespace std;
- int main() {
- int a=5;
- int *ptr;
- ptr=&a;
- cout << ptr << endl;
- }
- the first two statements of main()
- declares the two variables,
- one as "int"
- and the other as "integer pointer"
- in the third statement
- the pointer takes the memory address of the variable
- and in the last statement
- we print the pointer.
- That means that the result will show the memory address of int a
- usually it is shown as:
- 0x.....
- lets explain something else,
- in the *ptr, the memory address of int a is stored.
- So, if we want to print the value of int a.
- We can do this:
- #include<iostream>
- using namespace std;
- int main() {
- int a=5;
- int *ptr;
- ptr=&a;
- cout << *ptr << endl;
- }
- as you can see
- in the last statement
- there is a
- *
- in front of the pointer
- & hence it means that this code will print the value of int a (i.e. "5").
- Now if we want to change the value of int a itself
- (without doing this:
- a=75;
- // or something else)
- 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.
- For example:
- #include<iostream>
- using namespace std;
- int main() {
- int a=5;
- int *ptr;
- ptr=&a;
- *ptr=6;
- cout << a << endl;
- }
- as you can see
- in the statement
- *ptr=6;
- we put this
- *
- before the name of the pointer
- because we want to change the value of int a rather than to change the memory address of int a :P.
- And in that code, we tell the compiler to change the value of int a to "6".
- Now because this pointer has the memory address of int a, automatically the value of int a will become
- 6
- too.
- So, in the last statement it prints:
- 6
- Also please look at this interesting code for a moment:
- #include <iostream>
- using namespace std;
- int main() {
- int a=5;
- int *ptr;
- ptr=&a;
- (*ptr)++;
- cout << a << endl;
- // or
- // cout << *ptr << endl;
- }
- this will increment the value of int a by 1
- but the interesting thing in it are those "(" and ")"
- because if we don't put them, it will not work
- 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)
- So, it means that it will increment the value of int a rather than it's memory address.
- -- LECTURE ENDED --
- -- Note --
- I will talk about some strings-pointers in the next lesson.
- ** The End ;)
- ** Teacher: Hepic
- ** Moderator: XRahat2011
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement