Declare “a function with argument of int* which returns pointer to an array of 4 integer pointers”.
At the first glance it may look complex, we can declare the required function with a series of decomposed statements.
1. We need, a function with argument int *,
function(int *)
2. a function with argument int *, returning pointer to
(*function(int *))
3. a function with argument int *, returning pointer to array of 4
(*function(int *))[4]
4. a function with argument int *, returning pointer to array of 4 integer pointers
int *(*function(int *))[4];
How can we ensure that the above declaration is correct? The following program can cross checks our declaration,
#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 SIZE_OF_ARRAYÂ is used for symbolic representation of array size. p_array_t is typedefined as “pointer to an array of 4 integers”. If our declaration is wrong, the program throws an error at the ‘function‘ definition.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 May, 2017
Like Article
Save Article