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?
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?
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).
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
See cdecl
:
declare RT as pointer to function (int, int) returning pointer to void
RT
is a pointer to a function that takes two integers as arguments and returns a void *
(generic pointer).
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.
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);
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.