Complex pointer expressions

I read Thinking in C ++ vol1 , and there is a section called Complex declarations and definitions that displays the following expressions that I cannot understand:

void * (*(*fp1)(int))[10];
float (*(*fp2)(int,int,float))(int);

Can someone explain what the expressions mean, and how do you usually solve these expressions?

Bruce gave interpretations as follows:

fp1 is a pointer to a function that takes an integer argument and returns a pointer to an array of ten void pointers.

fp2is a pointer to a function that takes three arguments (int, int, and float)and returns a pointer to a function that takes an integer argument and returnsfloat

Although I posted the correct answers, I would appreciate it if someone demonstrated the decoding of these expressions step by step.

+4
source share
5 answers

Here's the rule: start with the variable name and scan until you reach )either the end of the declaration, then scan left until you reach (or start the definition, and then repeat.

, fp1. , ), . *, , fp1 . (, . - (, . int, int. , " , int". ), . *, . (, [10]. , 10. , , int 10. , void*. , " , int 10, void".

.

+5

void * (*(*fp1)(int))[10]; . X Y Z , .

void * (
  *(
    *fp1
  )
  (int)
)[10]

fp1 (*fp1)

void * (
  *X
  (int)
)[10]

, int X(int), :

void * (
  *Y
)[10]

*Y

void* Z[10]

[10], void*.

float (*(*fp2)(int,int,float))(int);

float (
  *(*fp2)(int,int,float)
)(int);

fp2 *fp2

float (
  *X(int,int,float)
)(int);

- , int, int, float

float (
  *Y
)(int);

:

float Z(int);

- , int a float.

, .

, , . (- , ).

, , , . , . , , , (, , , , . , ).

+3

, ,

, --- -...

,

void * (*(*fp1)(int))[10];
           ^^^

fp1 ...

void * (*(*fp1)(int))[10];
              ^

fp1 is... ( , )

void * (*(*fp1)(int))[10];
          ^

fp1 - ...

void * (*(*fp1)(int))[10];
               ^^^^^

fp1 - , ...

void * (*(*fp1)(int))[10];
        ^

fp1 - , ...

void * (*(*fp1)(int))[10];
                     ^^^^

fp1 - , 10...

void * (*(*fp1)(int))[10];
^^^^^^

fp1 void.

.

http://cdecl.org/, .

void * (*(*fp1)(int))[10];

fp1 (int), 10 void

+1

- , , ?

, . , " ". , , ; - , .

, , cdecl.org . : , , , , .

, cdecl . . , C ++ : , , .

" void", :

typedef void* (TenVoidPtrs)[10];

" , int void":

typedef TenVoidPtrs*(*RetPtrTenVoid)(int)

TenVoidPtrs . , , cdecl: , :

RetPtrTenVoid fPtr = ...
+1

this is not really an answer to how to disassemble it, but rather write instead. since C ++ 11 there is a slightly simpler way to create these typedefs

using Func_I_F = auto (*)(int) -> float;

using Func_IIF_FIF = auto (*)(int,int,float) -> Func_I_F;

Func_IIF_FIF fp2;

fp2 is of the same type as you wrote.

+1
source

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


All Articles