1#include <iostream>
2using namespace std;
3int main(){
4 //Pointer declaration
5 int *p, var=101;
6
7 //Assignment
8 p = &var;
9
10 cout<<"Address of var: "<<&var<<endl;
11 cout<<"Address of var: "<<p<<endl;
12 cout<<"Address of p: "<<&p<<endl;
13 cout<<"Value of var: "<<*p;
14 return 0;
15}
1int myvar = 6;
2int pointer = &myvar; // adress of myvar
3int value = *pointer; // the value the pointer points to: 6
1#include <iostream>
2
3using namespace std;
4
5int main () {
6 int var = 20; // actual variable declaration.
7 int *ip; // pointer variable
8
9 ip = &var; // store address of var in pointer variable
10
11 cout << "Value of var variable: ";
12 cout << var << endl; //Prints "20"
13
14 // print the address stored in ip pointer variable
15 cout << "Address stored in ip variable: ";
16 cout << ip << endl; //Prints "b7f8yufs78fds"
17
18 // access the value at the address available in pointer
19 cout << "Value of *ip variable: ";
20 cout << *ip << endl; //Prints "20"
21
22 return 0;
23}
1Every object in C++ has access to its own address through an important pointer called this pointer.
2 The this pointer is an implicit parameter to all member functions.
3Therefore, inside a member function,
4 this may be used to refer to the invoking object.
5
6Friend functions do not have a this pointer,
7 because friends are not members of a class.
8Only member functions have a this pointer.
1// pointer to functions
2#include <iostream>
3using namespace std;
4
5int addition (int a, int b)
6{ return (a+b); }
7
8int subtraction (int a, int b)
9{ return (a-b); }
10
11int operation (int x, int y, int (*functocall)(int,int))
12{
13 int g;
14 g = (*functocall)(x,y);
15 return (g);
16}
17
18int main ()
19{
20 int m,n;
21 int (*minus)(int,int) = subtraction;
22
23 m = operation (7, 5, addition);
24 n = operation (20, m, minus);
25 cout <<n;
26 return 0;
27}