What does double * (* p [3]) (void * (*) ()); I mean?

It’s hard for me to understand what the next announcement means. Is this a standard declaration?

double* (*p[3]) (void* (*)()); 

Can someone help me understand the meaning of this declaration?

+8
c pointers
source share
5 answers

Rule for reading hairy ads: find the leftmost identifier and work outside, remembering that () and [] bind to * , so T *a[N] is an array of pointers to T , T (*a)[N] is equal to a pointer to an array from T , T *f() is a function that returns a pointer to T , and T (*f)() is a pointer to a function that returns T. Since a function prototype can omit parameter names, you can see things like T *[N] or T (*)() . The value is basically the same as 1, just pretending to have an identifier of length 0.

Thus,

  p -- p p[3] -- is a 3-element array *p[3] -- of pointers (*p[3]) ( ) -- to functions (*p[3]) ( (*)()) -- taking a pointer to a function (*p[3]) ( * (*)()) -- returning a pointer (*p[3]) (void* (*)()) -- to void * (*p[3]) (void* (*)()) -- returning a pointer double* (*p[3]) (void* (*)()); -- to double 

It is important to distract from the fact that you declare p as an array ... rather than returning a function ...

What would a beast look like in practice? Well, firstly, you will need three functions. Each of these functions takes one parameter, which is a pointer to a function that returns a pointer to void:

 double *foo(void *(*)()); double *bar(void *(*)()); double *bletch(void *(*)()); double *(*p[3]) (void *(*)()) = {foo, bar, bletch}; 

Each of foo , bar and bletch will call the function passed to it and somehow return a pointer to double .

You would also like to define one or more functions that satisfy the type of parameters for each of foo , bar and bletch :

 void *blurga() {...} 

so if you call foo directly, you would name it as

 double *pv; ... pv = foo(blurga); 

So, we could imagine a challenge like

 double *pv = (*p[0])(blurga); 


1 - the difference lies in the fact that in the context of the declaration of the parameter of the function, T a[] and T a[N] identical to T *a ; in all three cases, a is a pointer to T , not an array of T Note that this is only true in the declaration of a function parameter. Thus, T *[] will be identical to T ** .
+9
source share

Just use http://cdecl.org :

declare p as an array of 3 pointers to a function (a pointer to a function that returns a pointer to void) returning a pointer to double

For more information, see the MSDN article: Interpreting more complex declarators .

But typedefs will help:

 typedef void *(*foo)(); // foo is a function-pointer type typedef double *(*bar)(foo); // bar is also a function-pointer type bar p[3]; 

(Obviously, instead of foo and bar !) Use the appropriate names

+10
source share

Your p is an array of 3 pointers to a function that returns a double pointer, and takes as argument an pointer to another function that returns a void pointer and takes no arguments.

But, do not use this syntax , try using typedef instead.

+2
source share

This is an array (size 3) of function pointers that returns a pointer to double and takes another function pointer as an argument.

Type of function whose pointer can be stored in an array: double *(func)(void* (*)())
The type of function whose pointer can be passed as an argument to the func function: void *(func1)(void)

+1
source share

β€œThere is a technology known as the Clockwise / Spiral Rule, which allows any C programmer to analyze any C declaration in his head!”

Clockwise - http://c-faq.com/decl/spiral.anderson.html

0
source share

All Articles