In this tutorial, we will learn how to declare a C/C++ function returning pointer to array of integer pointers.
Part 1: Create a function that considers an int* argument and generates a pointer to a list of four integer pointers.
Although it may appear difficult at first glance, we may declare the appropriate function using a sequence of deconstructed statements.
- We require a function with the argument int *.
function(int *)
- A function that takes an int * argument and returns a pointer to
(function(int ))
- a function with the argument int * that returns a pointer to a 4-array.
(function(int ))
- a function with the argument int *, which returns a pointer to an array of four integer pointers
int (function(int *))[4];
How can we be certain that the preceding declaration is completely right? The following program can validate our declaration.
Code(syntax):
#include<stdio.h>
#define SIZE_OF_ARRAY (4)
typedef int *(*p_array_t)[SIZE_OF_ARRAY];
int *(*function(int *arg))[4];
p_array_t function(int *arg)
{
static int *arr[SIZE_OF_ARRAY] = {NULL};
p_array_t pRet = &arr;
return pRet;
}
int main()
{
}
The macro SIZEOFARRAY serves to represent array size symbolically. The type definition of parrayt is "pointer to an array of four integers." If our declaration is incorrect, the program stops working at the definition of the 'function.'
Part 2: The aim is to write a function with the provided array that returns a pointer to an array of integer function pointers.
For that, we'll enter the two values, execute a function that compares them, then use a function pointer to get the memory location of the larger value and output the result. Functions are made more flexible and abstract by using the function pointer to provide addresses of various functions at various times. Therefore, by offering a straightforward method to choose a function to execute based on run-time information, function pointers can be utilised to simplify code.
Illustration of the big function
The function big is called by the application and receives two integer values by reference. It compares the two values and returns the memory address of the larger number. Big has an integer return value that can be either a non-zero or a zero number.
For instance,
Input ? 8 14
Output ? Bigger value is 14
Input ?5 9
Output? Bigger value is 9
Analysis: After comparing the two integer values that we had, the pointer will return the memory location with the biggest value.
A method that could be used
- Consider the integer pointer int *c.
- The two integer variables should be initialised.
- We will enter the two values.
- Comparing the two values provided.
- Pointer *c finally returns the address of a larger value.
Algorithm
Start
STEP 1-> Pass the argument to the function after creating it.
Int *big(int &, int &)
END
STEP 2-: calling the main() function to initialise the pointer *c and enter and output two values.
int p, q, *c
call c= big(p,q)
print c
END
STEP 3-> Pointer c returns the memory address of the larger value after comparing the two integer values given to it.
Comparing
If(a>b)
return(&a)
else
return(&b)
END
STOP
C++ Code:
#include<iostream.h>
int *big(int&, int&);
int main( ){
int p, q, *c;
c= big(3, 9);
cout<<"The bigger value is"<<*c;
";
return 0;
}
int *big(int&a, int&b){
if(a>b)
return(&a);
else
return(&b);
}
Output:
Running the above mentioned code will result in the output shown below.
The bigger value is 9