What does the following code do?

Possible duplicate:
How do you read C ads?

I do not understand the following:

int‬‬ ‪* (*(*p)[2][2])(int,int); 

You can help?

+4
source share
4 answers

For such things, try cdecl , decoded to;

 declare p as pointer to array 2 of array 2 of pointer to function (int, int) returning pointer to int 
+18
source
  p -- p *p -- is a pointer (*p)[2] -- to a 2-element array (*p)[2][2] -- of 2-element arrays *(*p)[2][2] -- of pointers (*(*p)[2][2])( ); -- to functions (*(*p)[2][2])(int,int); -- taking 2 int parameters * (*(*p)[2][2])(int,int); -- and returning a pointer int‬‬ ‪* (*(*p)[2][2])(int,int); -- to int 

What would a beast look like in practice?

 int *q(int x, int y) {...} // functions returning int * int *r(int x, int y) {...} int *s(int x, int y) {...} int *t(int x, int y) {...} ... int *(*fptr[2][2])(int,int) = {{p,q},{r,s}}; // 2x2 array of pointers to // functions returning int * ... int *(*(*p)[2][2])(int,int) = &fptr; // pointer to 2x2 array of pointers // to functions returning int * ... int *v0 = (*(*p)[0][0])(x,y); // calls q int *v1 = (*(*p)[0][1])(x,y); // calls r ... // etc. 
+16
source

Pretty sure that it defines p as a pointer to an array of pointers 2 through 2 (functions take (int a, int b) as parameters and return a pointer to int)

+4
source

The expression defines a pointer to an array of 2x2 function pointers. See http://www.newty.de/fpt/fpt.html#arrays for an introduction to C / C ++ functions (and their arrays) in pointers.

In particular, given the declaration of the function

 int* foo(int a, int b); 

You define a pointer to the ptr_to_foo function (and assign it the address foo) as follows:

 int* (*ptr_to_foo)(int, int) = &foo; 

Now, if you need not only one function pointer, but also an array of them (let it be a 2 x 2 2D array):

 int* (*array_of_ptr_to_foo[2][2])(int, int); array_of_ptr_to_foo[0][0] = &foo1; array_of_ptr_to_foo[0][1] = &foo2; /* and so on */ 

Apparently, this is not enough. Instead of an array of function pointers, we need a pointer to such an array. And it will be:

 int* (*(*p)[2][2])(int, int); p = &array_of_ptr_to_foo; 
+2
source

Source: https://habr.com/ru/post/1335527/


All Articles