Chapter 3. Pointers
Chapter 3. Pointers
CoSc 2031
Chapter 3
Pointer
Mekonnen K.
1
(MSc) Email: [email protected]
Objectives:
• Pointer Data Type and Pointer Variables
• Pointer Declaration
• Pointer Operators
• Initializing Pointer Variables
• Operations, Expressions and Arithmetic on Pointer Variables
• Strings and pointers
• Relationship between pointers & arrays
• Functions and Pointers
• Dynamic memory management
2
What is Pointer
3
Pointer Data Type and Pointer Variables
Simple
Structured
Pointers.
4
What is the Pointer Value
5
Pointer Data Type and Pointer Variables
6
Pointer Declaration
Thus, the character * can appear anywhere between the
data type name and the variable name.
7
Pointer Declaration
int* p, q;
In this statement:
only p is the pointer variable, not q.
int *p, q;
8
Pointer Operators
C++ provides two operators operator to work with
pointers.
9
Address of Operator (&)
is a unary operator that returns the address of its
operand.
For example, given the statements:
int x;
int *p;
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
10
Dereferencing Operator (*)
referred to as indirection operator
11
Dereferencing Operator (*)
Let us consider the following statements:
In these statements:
p is a pointer variable of type int
num is a variable of type int.
12
Dereferencing Operator (*)
1. num = 78;
2. p = #
3. *p = 24;
13
Example
#include <iostream>
#include <string>
using namespace std;
//===============================
int main() {
int *p;
int x = 37;
cout <<"x= " << x<<endl;
p=&x;
cout <<"*p= "<< *p <<" , x= "<<x<<endl;
*p=85;
cout <<"*P= " <<*p <<" , x= "<<x<<endl;
cout <<" Address of p is : "<< &p <<endl;
cout<<"value of p : "<< p<<endl;
cout<< " value of memory location pointed to by *p = "<<*p<<endl;
cout<<"Address of X = " << &x<<endl;
cout <<" Value of x = " << x<<endl;
return 0;
}
14
15
Summary:
16
Initializing Pointer Variables
C++ does not automatically initialize variables
pointer variables must be initialized if you do not want them
to point to anything.
Pointer variables are initialized using the following two
statements :
p = NULL;
p=0;
19
Operations on Pointer Variables
23
Cont.
Moreover, the statement:
p = p + 2;
increments the value of p by 8 bytes.
Thus, when an integer is added to a pointer variable, the value
of the pointer variable is incremented by the integer times the
size of the memory that the pointer is pointing to.
Similarly, when an integer is subtracted from a pointer variable,
the value of the pointer variable is decremented by the integer
times the size of the memory to which the pointer is pointing.
24
Notes
Pointer arithmetic can be very dangerous.
25
Example.
A program to illustrate the pointer expression and pointer arithmetic.
#include<iostream.h>
void main( )
{
int a, b, x, y;
int *ptr1, *ptr2;
a = 30;
b = 6;
ptr1 = &a
ptr2 = &b;
x = *ptr1 + *ptr2 – 6;
y = 6 - *ptr1 / *ptr2 + 30;
cout<<”Address of a = “<<ptr1<<endl;
cout<<”Address of b = “<<ptr2<<endl;
cout<<”a = “<<a<<”b = “<<b<<endl;
cout<<”x = “<<x<<”y = “<<y<<endl;
*ptr1 = *ptr1 + 70;
*ptr2 = *ptr2 * 2
26 cout<<”a = “<<a<<”b = “<<b<<endl;
}
Pointers and arrays
There is a close relationship between array and pointers in
C++.
Consider the following program which prints the elements of
an array A.
#include<iostream.h>
void main( )
{
int A[5] = { 15, 25, 67, 83, 12};
for (int i = 0; i<5; i++)
cout<<A[i]<<”\t”;
}
Output of the above program is: 15 25 67 83 12
27
Pointers and arrays
When we declare an array, its name is treated as a constant
pointer to the first element of the array.
This is also known as the base address of the array.
In other words base address is the address of the first element
in the array of the address of a[0].
If we use constant pointer to print array elements.
#include<iostream.h>
void main( )
{
int A[5] = { 15, 25, 67, 83, 12};
cout<< *(A) <<”\t”;
cout<< *(A+1) <<”\t”;
cout<< *(A+2) <<”\t”;
cout<< *(A+3) <<”\t”;
cout<< *(A+4) <<”\t”;
}
28
Output of the above program is: 15 25 67 83 12
Pointers and arrays
Here the expression *(A+3) has exactly same effect as A[3] in
the program.
29
Pointers and arrays
The following example shows the relationship between pointer and
one dimensional array.
#include<iostream.h>
void main( )
{
int a[10], i, n;
cout<<”Enter the input for array”;
cin>>n;
cout<<”Enter array elements:”;
for(i=0; i<n; i++)
cin>>*(a+i);
cout<<The given array elements are :”;
for(i=0; i<n; i++)
cout<<”\t”<<*(a+i);
}
30
Pointers and string
String is sequence of characters ends with null (“\0”) character.
33
Example
#include <iostream>
using namespace std;
void f(int *j);
int main()
{
int i =0 ;
int *p;
p = &i; // p now points to i
cout<<"*p is " << *p<<endl
<< "i= " << i <<endl;
f(p);
cout << "i is " << i << endl;
return 0;
}
void f(int *j)
{
*j = 100; // var pointed to by j is assigned 100
cout <<"*j is : "<< *j<<endl;
}
34
Functions and Pointers
Passing a pointer as an argument to a function is in some
ways similar to passing a reference.
35
Pointers and Function Return Values
In C++, the return type of a function can be a pointer. For
example, the return type of the function:
36
Example
#include <iostream>
using namespace std;
double * GetSalary()
{
double salary = 26.48;
double *HourlySalary = &salary;
return HourlySalary;
}
int main()
{
double hours = 46.50;
double salary = *GetSalary();
cout << "Weekly Hours: " << hours << endl;
cout << "Hourly Salary: " << salary << endl;
double WeeklySalary = hours * salary;
cout << "Weekly Salary: " << WeeklySalary << endl;
return 0;
}
37
38
Memory Allocation of Pointers
Memory Allocation is done in two ways:
o Static Allocation of memory
o Dynamic allocation of memory.
Example:
39
Memory Allocation of Pointers
Dynamic Allocation of Memory:
Dynamic memory allocation refers to the process of allocating
memory during the execution of the program or at run time.
Memory space allocated with this method is not fixed.
C++ supports dynamic allocation and de-allocation of objects using
new and delete operators.
These operators allocate memory for objects from a pool called the
free store.
The new operator calls the special function operator new and
delete operators call the special function operator delete.
40
New Operator
We can allocate storage for a variable while program is
running by using new operator.
It is used to allocate memory without having to define
variables and then make pointers point to them.
The following code demonstrates how to allocate memory for
different variables.
To allocate memory type integer
int * pnumber;
pnumber = new int;
41
New Operator
The first line declares the pointer, pnumber. The second line
then allocates memory for an integer and then makes
pnumber point to this new memory.
42
Delete Operator
The delete operator is used to destroy the variables space
which has been created by using the new operator dynamically.
43
Difference between Static and Dynamic
Memory Allocation
44
Free store (Heap memory)
Free store is a pool of memory available to allocated and
de-allocated storage for the objects during the execution
of the memory.
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 increases in number,
bring adverse effect on the system. This situation is called
memory leak.
45
46