Declare a C/C++ function returning pointer to array of integer function pointers


With given array the task is to create a function which will return pointer to an array of integer function pointers.

For that we will input the two values and call a function which compares both the two values and functions pointer which return the memory address of bigger value and print it as a result. The function pointer is used to pass address of different function at different times thus making the function more flexible and abstract. So function pointers can be used to simplify code by providing a simple way to select a function to execute based on run-time values.

Explanation of function big()

The program passes two integers by reference to the function big( ) that compares the two integer values passed to it and returns the memory address of the bigger value. The return value of big( ) is of integer type which can be a non-zero as well as zero value.

For example

Input − 7 13

Output − Bigger value is 13

Input −8 6

Output− Bigger value is 8

Explanation − we had two integer values and after comparing, the pointer will return the memory address of the largest value amongst the two.

Approach that can be followed

  • Take the integer pointer let’s say, int *c.
  • Then initialize the two integer variables.
  • After that we will input the two values.
  • Compare the two given values.
  • At last pointer *c return the address of bigger value.

Algorithm

Start
STEP 1-> Create the function and pass the argument.
   Int *big(int &, int &)
END
STEP 2-: call the main() function for entering and printing two values and initialize the pointer *c.
   int a, b, *c
   call c= big(a,b)
   print c
END
STEP 3-> compare the two Integer values passed to it and returns the memory address of the bigger value through pointer c.
   Comparing
   If(x>y)
      return(&x)
   else
      return(&y)
   END
STOP

Example

#include<iostream.h>
Int *big(int&, int&);
Int main( ){
   Int a, b, *c;
   c= big(4, 7);
   cout<<”The bigger value is”<<*c<<”\n”;
   return 0;
}
Int *big(int&x, int&y){
   If(x>y)
      return(&x);
   else
      return(&y);
}

Output

If we run the above program then it will generate following output

The bigger value is 7
The bigger value is 5

Updated on: 27-Feb-2020

390 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements