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

Pointers To Functions

The document contains examples of using pointers in C++. It demonstrates: 1) Defining a function pointer and calling functions through a function pointer. 2) Declaring an array of pointers to strings and comparing a string to those in the array. 3) Using pointers with a class to dynamically allocate objects and call methods on the objects. 4) Implementing bubble sort using pointers by passing arrays of integers to sorting functions. 5) Printing the memory addresses of variables to demonstrate how pointers store addresses.

Uploaded by

rahul-singh-6592
Copyright
© Attribution Non-Commercial (BY-NC)
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)
55 views6 pages

Pointers To Functions

The document contains examples of using pointers in C++. It demonstrates: 1) Defining a function pointer and calling functions through a function pointer. 2) Declaring an array of pointers to strings and comparing a string to those in the array. 3) Using pointers with a class to dynamically allocate objects and call methods on the objects. 4) Implementing bubble sort using pointers by passing arrays of integers to sorting functions. 5) Printing the memory addresses of variables to demonstrate how pointers store addresses.

Uploaded by

rahul-singh-6592
Copyright
© Attribution Non-Commercial (BY-NC)
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

//Pointers to functions

#include<iostream.h>
#include<conio.h>
typedef void (*funptr)(int, int);
void add(int i, int j)
{
cout<<i<<"+"<<j<<"="<<i+j;
}
void sub(int i, int j)
{
cout<<i<<"-"<<j<<"="<<i-j;
}
void main()
{
funptr ptr;
ptr= &add;
ptr(1,2);
cout<<endl;
ptr= &sub;
ptr(3,2);
getch();
}

OUTPUT:
1+2=3
3-2=1
//Array of pointers

#include<iostream.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
void main()
{
int i=0;
char *ptr[10]={"books","television", "computer", "sports"};
char str[25];
clrscr();
cout<<"\n enter ur favorite leisure pursuit: ";
cin>>str;
for(i=0;i<4;i++)
{
if(!strcmp(str, *ptr[i]))
{
cout<<"\n ur favorite pursuit "<<" is available"<<endl;
break;
}
}
if(i==4)
cout<<"\n not available";
getch();
}

OUTPUT:
enter ur favorite leisure pursuit: books
ur favorite pursuit is available
//Pointers and objects

#include<iostream.h>
#include<conio.h>
class distance
{
private:
int feet;
float inches;
public:
void getdist()
{
cout<<"\n enter the feet";
cin>>feet;
cout<<"\n enter inches";
cin>>inches;
}
void showdist()
{
cout<<feet<<"\'"<<inches;
}
};
void main()
{
clrscr();
distance dist;
dist.getdist();
dist.showdist();
distance* distptr;
distptr = new distance;
distptr->getdist();
distptr->showdist();
getch();
}
OUTPUT:

enter the feet4

enter inches7
4'7
enter the feet6

enter inches9
6'9
//Bubble sorting using pointers

#include<iostream.h>
#include<conio.h>
void main()
{
void bsort(int*, int);
const int n=10;
int arr[n]={37,84,62,91,11,65,57,28,19,49};
bsort(arr,n);
for(int j=0;j<n;j++)
cout<<arr[j]<<" ";getch();
}
void bsort(int* ptr,int n)
{
void order(int* ,int*);
int j,k;
for(j=0;j<n-1;j++)
for(k=k+1;k<n;k++)
order(ptr+j, ptr+k);
}
void order(int *numb1,int *numb2)
{
if(*numb1 > *numb2)
{
int temp = *numb1;
*numb1 = *numb2;
*numb2 = temp;
}
}

OUTPUT:
11 19 28 37 49 57 62 65 84 91
//Address and pointers

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int var1=11;
int var2=44;
cout<<endl<<&var1;
cout<<endl<<&var2;
getch();
}

OUTPUT:

0x8fd6fff4
0x8fd6fff2

You might also like