user707549
user707549

Reputation:

the typedef of pointer in c language

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

Answers (8)

John Bode
John Bode

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

Remo.D
Remo.D

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:

  • 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).

Upvotes: 6

Armen Tsirunyan
Armen Tsirunyan

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

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84159

See cdecl:

declare RT as pointer to function (int, int) returning pointer to void

Upvotes: 2

Constantinius
Constantinius

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

Michael Mior
Michael Mior

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

sergio
sergio

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

Jonathan Leffler
Jonathan Leffler

Reputation: 753695

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

Upvotes: 1

Related Questions