0% found this document useful (0 votes)
0 views6 pages

Chapter 5 - PointersConcise

This document provides an overview of pointers in C++, explaining their definition, usage, and examples of pointer operations including dynamic memory allocation and pointer arithmetic. It includes code snippets demonstrating how to use pointers to access and manipulate data, as well as how to perform operations like swapping values and calculating sums. Additionally, it covers the concept of function pointers and their application in passing arguments by value and by reference.

Uploaded by

kahsay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views6 pages

Chapter 5 - PointersConcise

This document provides an overview of pointers in C++, explaining their definition, usage, and examples of pointer operations including dynamic memory allocation and pointer arithmetic. It includes code snippets demonstrating how to use pointers to access and manipulate data, as well as how to perform operations like swapping values and calculating sums. Additionally, it covers the concept of function pointers and their application in passing arguments by value and by reference.

Uploaded by

kahsay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1|Page Assosa University Department Of Computer

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 = &num;
p2= &num;
sq=&num;

cout<<"\n Value of num: "<<*p1; // Content of num 25


cout<<"\n Value of num: "<<*p2; // Content of num 25
cout<<"\n Value of num: "<<*sq; // Content of num 25
cout<<"\n SQRT of num: "<<sqrt(*p1); // Square root of num 5
cout<<"\n SQRT of num: "<<sqrt(*p2); // Square root of num 5
cout<<"\n SQRT of num: "<<sqrt(*sq); // Square root of num 5
return 0;
}
Example 2: Write C++ Program that displays contents of array
#include<iostream.h>
#include<math.h>
int main()
{
Chapter 5 Pointers
Compiled by Gebreigziabher A.
2|Page Assosa University Department Of Computer
Science

int N[] = { 10,20,30,40,50,60};


int *ptr = N; // or int *ptr = &N[0];
cout<<"\n Displaying array contents using array\n ";
for(int i=0; i<6; i++)
{
cout<<N[i]<<" "; //using array
}
cout<<"\n Displaying array contents using pointer\n ";
for(int i=0; i<6; i++)
{
cout<<*(ptr+i)<<" "; //using pointer
}

cout<<"\n Displaying array contents using pointer\n ";

for( ptr= &N[0]; ptr<=&N[5]; ptr++)


{
cout<<*ptr<<" "; //using pointer
}
return 0;
}

Dynamic Memory Allocation (DMA)


Two operators are used for allocating and deallocating memory blocks. The
new and delete operators. The new operator takes a type as argument and
allocated a memory block for an object of that type. It returns a pointer to the
allocated block. For example,
int *ptr = new int; // Allocate a block for storing a single integer
char *str = new char[10]; //Allocate a block large enough for storing an array
of 10 characters.
The delete operator is used for releasing memory blocks allocated by new. It
takes a pointer as argument and releases the memory block to which it points.
For example:
delete ptr; // delete an object
delete [] str; // delete an array of objects
Note that when the block to be deleted is an array, an additional [] should be
included to indicate.
It is harmless to apply delete to the 0 pointer.
Example of DMA
Write C++ program that performs addition of two numbers and outputs the
sum using DMA

//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 = &sum;
cout<<”\n Enter Value of x: ”;
cin>>*p1;

cout<<”\n Enter Value of y: ”;


cin>>*p2;
*s = *p1+*p2;
cout<<”\n The Sum x+ y= ”<<*s;
return 0;
}
Function Pointers
It is possible to take the address of a function and store it in a function
pointer. The pointer can then be used to indirectly call the function.

Chapter 5 Pointers
Compiled by Gebreigziabher A.
5|Page Assosa University Department Of Computer
Science

//Demonstration of Passing Pointers by Value and Pass by Reference


#include<iostream.h>
void Swap1 (int x, int y) // pass-by-value (objects)
{
int temp = x;
x = y;
y = temp;
}
void Swap2 (int *x, int *y) // pass-by-value (pointers)
{
int temp = *x;
*x = *y;
*y = temp;
}
void Swap3 (int &x, int &y) // pass-by-reference (address)
{
int temp = x;
x = y;
y = temp;
}
int main (void) //void indicates the function take no argument
{
int i = 10, j = 20;
Swap1(i, j); cout << i << ", " << j << '\n';
Swap2(&i, &j); cout << i << ", " << j << '\n';
Swap3(i, j); cout << i << ", " << j << '\n';
}
//Output
10,20
20,10
10,20

Chapter 5 Pointers
Compiled by Gebreigziabher A.
6|Page Assosa University Department Of Computer
Science

Chapter 5 Pointers
Compiled by Gebreigziabher A.

You might also like