2. Topics
• Pointers
• Memory addresses
• Indirection (*) Operators
• How to initialize pointers
• NULL pointer
• Pointers to pointer
• Array Of Pointer
3. Pointers
A pointer is a reference to another variable (memory location) in a
program
• Used to change variables inside a function (reference parameters)
• Used to remember a particular member of a group (such as an array)
• Used in dynamic (on-the-fly) memory allocation (especially of
arrays)
• Used in building complex data structures (linked lists, stacks, queues,
trees, etc.)
5. Indirection (*) Operators
• A pointer variable contains a memory address To refer to
the contents of the variable that the pointer points to, we
use indirection operator
• Syntax: *PointerVariable
Example:
int V = 101;
int *P = &V;
/* Then *P would refer to the contents of the variable V (in this case, the
integer 101) */
printf(“%d”,*P); /* Prints 101 */
6. How to initialize pointers :
• The process of assigning address of a variable to a pointer variable is known as
initialization
{ int a;
int *p; //declaration
p= &a; } //initialization
8. NULL pointer
• NULL is a special value that indicates an empty pointer
• If you try to access a NULL pointer, you will get an error
int *p;
p = 0;
cout << p << endl; //prints 0
cout << &p << endl;//prints address of p
cout << *p << endl;//Error!
9. Pointer to Pointer……
A pointer can also be made to point to a pointer variable (but the pointer must be of a
type that allows it to point to a pointer)
10. Example
1. #include <iostream>
2. using namespace std;
3. int main ()
4. { int var = 20; // actual variable declaration.
5. int *ip; // pointer variable
6. ip = &var; // store address of var in pointer variable
7. cout << "Value of var variable: ";
8. cout << var << endl;
9. // print the address stored in ip pointer variable
10. cout << "Address stored in ip variable: ";
11. cout << ip << endl;
12. // access the value at the address available in pointer
13. cout << "Value of *ip variable: ";
14. cout << *ip << endl;
15. return 0;
16. }
11. Output
• Value of var variable: 20
• Address stored in ip variable: 0xbfc601ac
• Value of*ip variable: 20
12. Array Of Pointer
• A common use of array of pointers is an array of pointers to strings.
• Like any other array, we can also have an array of pointers
• C++ treats the name of an array as constant pointer which contains base
address i.e. address of first location of array.
For Example:
int x[10];
• Here x is a constant pointer which contains the base address of the array x.
13. • We can also store the base address of the array in a pointer variable. It can be used to
access elements of array, because array is a continuous block of same memory
locations.
For Example:
int x[5];
int * ptr=x; // ptr will contain the base address of x
• we can also write
• int * ptr= &x[0]; //ptr will contain the base address of x
0
1
2
3
4
ptr
x
14. Array Name is a pointer constant
#include <iostream>
using namespace std;
void main (){
int a[5];
cout << "Address of a[0]: " << &a[0] << endl
<< "Name as pointer: " << a << endl;
}
Result:
Address of a[0]: 0x0065FDE4
Name as pointer: 0x0065FDE4