Reputation:
I have a question about typedef in c language. I read the following code:
typedef void* (*RT) (int a, int b);
what is typedefed in this example?
Upvotes: 2
Views: 653
Reputation: 123458
This declaration creates RT
as a typedef name (synonym) for the type "pointer to function taking two int
parameters and returning pointer to void
". You would then use RT
to declare objects of that type, like so:
RT foo, bar;
as opposed to writing
void *(*foo)(int a, int b), *(*bar)(int a, int b);
or
void *(*foo)(int a, int b);
void *(*bar)(int a, int b);
Upvotes: 1
Reputation: 16512
I suggest you to use the good old "spiral method":
+---------------------------------+
| +-------------------+ |
| | +----+ | |
| | | | | |
V V V | | |
typedef void * ( * RT | ) (int a, int b); |
| | | | ^ |
| | +--+ | |
| +---------------+ |
+------------------------------+
following the line you can read:
You start from the type name and move alternatively right and then left (respecting the parenthesis as in the example).
Upvotes: 6
Reputation: 132994
RT // RT
(*RT) // is a pointer to
(*RT) ( // a function
(*RT) (int a, int b); // taking two ints and returning
* (*RT) (int a, int b); // a pointer to
void* (*RT) (int a, int b); // void
Upvotes: 4
Reputation: 84159
See cdecl
:
declare RT as pointer to function (int, int) returning pointer to void
Upvotes: 2
Reputation: 35059
You are typedefing a function pointer. RT
is the name of the typedef, void*
its return type, and two times int
are the argument types of the function.
Upvotes: 1
Reputation: 28752
This will create a typedef for a function pointer to the name RT
. This is often used for callback functions in libraries. So when a callback functions is required, the function signature can be more concisely written with *RT
instead of the full signature.
Upvotes: 0
Reputation: 69027
This is pointer to a function returning void and taking two int argument. The name of the type is RT
.
When you are in such doubts, a very useful tool is cdecl.org.
Upvotes: 4
Reputation: 753695
RT
is a pointer to a function that takes two integers as arguments and returns a void *
(generic pointer).
Upvotes: 1