Chapter 5 - PointersConcise
Chapter 5 - PointersConcise
Science
Chapter Five
Pointers
______________________________________________________________________________
A pointer is simply the address of a memory location and provides an indirect
way of accessing data in memory. A pointer variable is defined to ‘point to’
data of a specific type. For example:
int *ptr1; // pointer to an int
char *ptr2; // pointer to a char
The value of a pointer variable is the address to which it points. For example,
given the definitions
int num; //can be written as ptr1 = #
The symbol & is the address operator; it takes a variable as argument and
returns the memory address of that variable. The assignment ptr1 = # is
that the address of num is assigned to ptr1. Therefore, ptr1 points to num.
Diagrammatically,
Given that ptr1 points to num, the expression *ptr1 dereferences ptr1 to get
to what it points to, and is equivalent to num.
The symbol * is the dereference operator; it takes a pointer as argument and
returns the contents of the location to which it points.
Example 1: Write C++ Program that finds the square of a number
//Demonstration of Pointers
#include<iostream.h>
#include<math.h>
int main()
{
double num=25;
double *p1, *p2, *sq;
p1 = #
p2= #
sq=#
//Demonstration of DMA
#include<iostream.h>
Chapter 5 Pointers
Compiled by Gebreigziabher A.
3|Page Assosa University Department Of Computer
Science
int main()
{
int *x = new int; // allocate memory for single int
int *y = new int; // " " "
int *sum = new int;
*x=5, *y=7;
*sum = *x+*y;
cout<<"\n Sum="<<*sum;
delete x;
delete y;
delete sum;
return 0;
}
Write C++ program that accepts array elements and displays them to console
using DMA
//Demonstration of DMA
//Demonstration of DMA
#include<iostream.h>
int main()
{
int *N = new int[20]; // Allocate memory for 20 ints dynamically
int *n = new int; // Allocate memory for single int dynamically
cout<<"\n Enter the number of Array elements: ";
cin>>*n;
cout<<"\n Inserting array contents using DMA\n ";
for(int i=0;i<*n;i++)
{
cout<<"\n Enter Element "<<i+1<<": ";
cin>>N[i];
}
cout<<"\n Displaying array contents using DMA\n ";
for(int i=0;i<*n;i++)
{
cout<<N[i]<<" ";
}
delete [] N;
delete n;
return 0;
}
Pointer Arithmetic
Pointer arithmetic means performing mathematical calculations using
pointers but not the same as integer arithmetic, because the outcome depends
on the size of the object pointed to. For example, an int is represented by 4
bytes. Now, given
char *str = "HELLO";
int nums[] = {10, 20, 30, 40};
int *ptr = &nums[0]; // pointer to first element
Chapter 5 Pointers
Compiled by Gebreigziabher A.
4|Page Assosa University Department Of Computer
Science
str++ advances str by one char (i.e., one byte) so that it points to the second
character of "HELLO", whereas ptr++ advances ptr by one int (i.e., four bytes)
so that it points to the second element of nums.
The elements of "HELLO" can be referred to as *str, *(str + 1), *(str + 2), etc.
Similarly, the elements of nums can be referred to as *ptr, *(ptr + 1), *(ptr + 2),
and *(ptr + 3).
Another form of pointer arithmetic allowed in C++ involves subtracting two
pointers of the same type. For example:
int *ptr1 = &nums[1];
int *ptr2 = &nums[3];
int n = ptr2 - ptr1; // n becomes 2
//Addition of two numbers using pointers
#include<iostream.h>
int main() {
int x,y,sum=0;
int *p1 = &x,*p2 = &y;
int *s = ∑
cout<<”\n Enter Value of x: ”;
cin>>*p1;
Chapter 5 Pointers
Compiled by Gebreigziabher A.
5|Page Assosa University Department Of Computer
Science
Chapter 5 Pointers
Compiled by Gebreigziabher A.
6|Page Assosa University Department Of Computer
Science
Chapter 5 Pointers
Compiled by Gebreigziabher A.