2

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?

  • 2
    This question gives you the info and links to be able to decipher yourself: http://stackoverflow.com/questions/89056/how-do-you-read-c-declarations Teach a man to fish and all that... – Mitch Wheat Jul 25 '11 at 13:55

8 Answers8

6

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:

  • RT is ...
  • a pointer to ...
  • a function returning ...
  • a pointer to ...
  • void

You start from the type name and move alternatively right and then left (respecting the parenthesis as in the example).

Remo.D
  • 16,122
  • 6
  • 43
  • 74
  • Never heard of this, interesting. – Matěj G. Jul 25 '11 at 15:01
  • Was published in an issue of the good old "Computer Language". Actually the "spiral" is an addition by myself but the concept is theirs. I found I remembered better this way. – Remo.D Jul 26 '11 at 20:30
4

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.

sergio
  • 68,819
  • 11
  • 102
  • 123
4
        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
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
2

See cdecl:

declare RT as pointer to function (int, int) returning pointer to void
Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
1

RT is a pointer to a function that takes two integers as arguments and returns a void * (generic pointer).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

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.

Constantinius
  • 34,183
  • 8
  • 77
  • 85
1

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);
John Bode
  • 119,563
  • 19
  • 122
  • 198
0

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.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113