C++ Pointers
C++ Pointers
Eng:Alhadi 1
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Pointers in C++
4
Address of Operator (&):- 1
In C++, the ampersand, &, called the address of operator, is a unary operator
that the statement:
p = &x;
assigns the address of x to p. That is, x and the value of p refer to the same
memory location.
Eng:Alhadi 2
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Pointers in C++
Eng:Alhadi 3
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Pointers in C++
POINTER
A pointer is a variable that holds a memory address, usually the location of
Eng:Alhadi 4
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Pointers in C++
Null Pointers
It is always a good practice to assign the pointer NULL to a pointer variable
in case you do not have exact address to be assigned. This is done at the time of
variable declaration. A pointer that is assigned NULL is called a null pointer.
The NULL pointer is a constant with a value of zero defined in several
standard libraries, including iostream. Consider the following program:
#include <iostream>
using namespace std;
int main ()
{
int *ptr = NULL;
Eng:Alhadi 5
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Pointers in C++
Memory layout
To access num using iptr and indirection operator *
cout << iptr; //
prints0x4a00 cout << *itptr;//
prints 25
#include <iostream>
using namespace std;
int main()
{ int value = 26;
int *pointer;
pointer = &value;
cout << " Value = " << value << "\n";
Eng:Alhadi 6
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Pointers in C++
Just as demonstrated earlier, after initializing a pointer, if you change the value of the
variable it points to, the pointer would be updated. Consider the following program:
#include <iostream>
using namespace std;
int main()
{ int value = 26;
int *pointer;
int **pointerToPointer;
pointer = &value;
pointerToPointer = &pointer;
Eng:Alhadi 7
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Pointers in C++
Pointer Arithmetic
Some arithmetic operators can be used with pointers:
- Increment and decrement operators ++, --
-Integer can be added to or subtracted from pointers using the pointers
using the operaters +,-,+=,and -=
Eng:Alhadi 8
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Pointers in C++
What is ptr+1?
It means (address in ptr) + (1 * size of an int)
cout<<*(ptr+1);// display 7
cout<<*(ptr+2);// display 11
Example :here complete code for float array and access to it’s elements by
pointer
#include <iostream>
#include <stdlib.h>
int main()
{float arr[]={30.5,44.7,65.7,88.9,90.2,77.9,69.69};
float *ptr = arr;
cout<<*(ptr)<<endl;// display
cout<<*(ptr+2)<<"\n";// display 3th array element
cout<<*(ptr+6)<<"\n";// display 7th array element
system("PAUSE");
return 0;
}
Example :complete code for insert integer array have eight elements ,print out
the elements ,print out the elements using pointer and display the memory
address for each array elements.
//program to Array Access & print out the array elments using pointers
// print out each array element address
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{int vls[8];
int *ptr1 = vls;
cout<<" insert eight array elements"<<endl;
for(int i=0;i<8;i++){
cout<<"a["<<i<<"] = ";
cin>>vls[i];}
Eng:Alhadi 9
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Pointers in C++
Array Access
Array notation arr[i] is equivalent to the pointer notation *(arr + i)
Assume the variable definitions
int arr[]={4,7,11};
int *ptr = arr;
Examples of use of ++ and -- ptr++; // points at 7
Eng:Alhadi 10
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Pointers in C++
int *ptr=var;
// let us have address of the last element in pointer. ptr = &var[MAX-1];
for (int i = MAX; i > 0; i--)
{cout << "Address of var[" << i << "] = ";
cout << (ptr-(i-1)) << endl;
cout << "Value of var[" << i << "] = ";
cout << *(ptr+i-1) << endl;// point to the previous location ptr--;
cout<<endl;
}
system("PAUSE");
return 0;
}
After execute the program result will be
Eng:Alhadi 11
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Pointers in C++
Eng:Alhadi 12
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Pointers in C++
#include<iostream>
using namespace std;
void swap(int *, int *);
int main()
{int a=10,b=20;
cout<< "value of a and b before sawpping a="<<a<<" b= "<<b<<endl;
swap(&a, &b); // calling swap function
cout<< "value of a and b after sawpping a="<<a<<" b= "<<b<<endl;
system("pause");
return 0;
}
void swap(int *x, int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
Pointers to Constants and Constant Pointers
Pointer to a constant: cannot change the value that is pointed at Constant
pointer: address in pointer cannot change once pointer is initialized
Pointers to Structures
We can create pointer to structure variables
struct student{ int rollno;float fees;};
student *stuptr=&stu1;
(*stuptr).rollno=104;
Static allocation of memory
In the static memory allocation, the amount of memory to be allocated is
predicted and preknown. This memory is allocated during the compilation
itself.
All the declared variables declared normally, are allocated memory statically.
Eng:Alhadi 13
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Pointers in C++
Memory Leak
If the objects, that are allocated memory dynamically, are not deleted using
delete, the memory block remains occupied even at the end of the program.
Such memory blocks are known as orphaned memory blocks. These orphaned
memory blocks when increase in number, bring adverse effect on the system.
This situation is called memory leak
*After using a pointer, don't forget to clean your memory. You do this using the
delete operator
Eng:Alhadi 14
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Pointers in C++
Eng:Alhadi 15
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Pointers in C++
Eng:Alhadi 16