Declare a C/C++ Function Returning Pointer to Array of Integer Pointers
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.
1. We require a function with the argument int *.
2. A function that takes an int * argument and returns a pointer to
3. a function with the argument int * that returns a pointer to a 4-array.
4. a function with the argument int *, which returns a pointer to an array of four integer pointers
int *(*function(int *));
How can we be certain that the preceding declaration is completely right? The following program can validate our declaration.
The macro SIZE_OF_ARRAY serves to represent array size symbolically. The type definition of p_array_t 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.
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
Running the above mentioned code will result in the output shown below.
The bigger value is 9