Javatpoint Logo
Javatpoint Logo

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 *.

function(int *)

2. A function that takes an int * argument and returns a pointer to

(*function(int *))

3. a function with the argument int * that returns a pointer to a 4-array.

(*function(int *))

[4]

4. 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):

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.

For instance,

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

C++ Code:

Output:

Running the above mentioned code will result in the output shown below.

The bigger value is 9






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA