Pointer to function returning function pointer

Christian Rau

I would like to declare a variable of type pointer to function returning pointer to function. Essentially what the following does, but without any typedefs:

typedef void (*func)();
typedef func (*funky_func)();

funky_func ptr;

I tried the following

(void (*)()) (*ptr)();

but it gives an "undeclared identifier"-error for ptr (probably due to completely different parsing). Being not that well-versed in the intricacies of parsing C++, I'd like to know if this is even possible and if yes, how to do it.

(Please consider this an entirely artificial scenario for the sake of curiosity, without any practical reason. I am perfectly aware that in practice typedefs are the way to go here, if using function pointers at all.)

Angew is no longer proud of SO

The general rule of C (and C++) declarations is: if you type the declaration as an expression, it will have the declaration's type.

So, you want a pointer to function which returns pointer to function returning void.

Let's say we have such a pointer, ptr. How to get void out of it?

  1. Dereference ptr, getting a function returning pointer to function returning void: *ptr

  2. Call the function, getting a pointer to function returning void: (*ptr)()

  3. Dereference that pointer, getting a function returning void: *(*ptr)()

  4. Call that function, getting void: (*(*ptr)())()

Now just turn this into a declaration:

void (*(*ptr)())();

P.S. I know other have answered in the meantime (and I've upvoted). But I wanted to show the generic process to arrive at the declaration form.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Function returning pointer to string

분류에서Dev

Returning a pointer to a structure from function

분류에서Dev

Function returning pointer called twice from main()

분류에서Dev

Function to Return Node Pointer

분류에서Dev

Pointer to array of struct in function

분류에서Dev

Pointer to function (list)

분류에서Dev

Passing a pointer to a function

분류에서Dev

What is the difference between returning a pointer to a calloced int and returning the address of a initialized int in a function?

분류에서Dev

Change address of kernel function pointer

분류에서Dev

when to pass a pointer argument to a function

분류에서Dev

When to use "this" pointer in member function

분류에서Dev

Ambiguous overload on function pointer and std::function

분류에서Dev

Wrap c++ function that needs function pointer

분류에서Dev

called object is not a function or function pointer in use of ternary

분류에서Dev

How to execute member-function-pointer with a smart pointer object?

분류에서Dev

Accessing a triple pointer inside a function with array notation

분류에서Dev

Using a pointer to function pointers without typedef?

분류에서Dev

How to get a pointer on the return value of a function?

분류에서Dev

C++: Pointer value changed exiting a function

분류에서Dev

Passing Array of Structs to a Function as a Pointer (C)

분류에서Dev

Allocating memory for a pointer inside a function call in C

분류에서Dev

getting address of pointer from function return value

분류에서Dev

Passing a pointer to a char array as an argument to a function - C

분류에서Dev

Moving pointer from main() to first executable function

분류에서Dev

Function pointer declaration works in C but not in C++

분류에서Dev

c++ return a pointer to a struct from a function

분류에서Dev

Why the function pointer pointing to different function works than it is defined

분류에서Dev

Passing a pointer to one C function as a parameter to another function

분류에서Dev

Can I move a function in C by copying the data a function pointer points to?

Related 관련 기사

  1. 1

    Function returning pointer to string

  2. 2

    Returning a pointer to a structure from function

  3. 3

    Function returning pointer called twice from main()

  4. 4

    Function to Return Node Pointer

  5. 5

    Pointer to array of struct in function

  6. 6

    Pointer to function (list)

  7. 7

    Passing a pointer to a function

  8. 8

    What is the difference between returning a pointer to a calloced int and returning the address of a initialized int in a function?

  9. 9

    Change address of kernel function pointer

  10. 10

    when to pass a pointer argument to a function

  11. 11

    When to use "this" pointer in member function

  12. 12

    Ambiguous overload on function pointer and std::function

  13. 13

    Wrap c++ function that needs function pointer

  14. 14

    called object is not a function or function pointer in use of ternary

  15. 15

    How to execute member-function-pointer with a smart pointer object?

  16. 16

    Accessing a triple pointer inside a function with array notation

  17. 17

    Using a pointer to function pointers without typedef?

  18. 18

    How to get a pointer on the return value of a function?

  19. 19

    C++: Pointer value changed exiting a function

  20. 20

    Passing Array of Structs to a Function as a Pointer (C)

  21. 21

    Allocating memory for a pointer inside a function call in C

  22. 22

    getting address of pointer from function return value

  23. 23

    Passing a pointer to a char array as an argument to a function - C

  24. 24

    Moving pointer from main() to first executable function

  25. 25

    Function pointer declaration works in C but not in C++

  26. 26

    c++ return a pointer to a struct from a function

  27. 27

    Why the function pointer pointing to different function works than it is defined

  28. 28

    Passing a pointer to one C function as a parameter to another function

  29. 29

    Can I move a function in C by copying the data a function pointer points to?

뜨겁다태그

보관