-1

While exploring some code, I found the following in a header file:

typedef void (*print_type) (const char*);

What does this line mean? I know that typedef is used to make specific word (in our case *print_type) to represent a specific type (in our case const char*).

But my question is why we have to use void?

AAEM
  • 1,837
  • 2
  • 18
  • 26
  • It is a function pointer and has nothing to do with a DLL. – Yashas Dec 12 '17 at 19:44
  • 1
    _"What does this line mean?"_ It's a function pointer type alias definition for functions with the signature `void fn (const char*);` – user0042 Dec 12 '17 at 19:45
  • how can i modify the question to correct its bad effect on the site and modify the bad voting?? – AAEM Apr 17 '18 at 09:15

3 Answers3

3

What is it?

typedef void (*print_type) (const char*);

The typedef tells the compiler that the data type print_type is a pointer to a function which accepts const char* as an argument and does not return a value.

... and it has nothing to do with a DLL


Function Pointers

We declare a function as follows:

ret-type identifier(parameters)

int f(double x)

Given that for something declared as int a, the pointer to it is declared as int *a (adding a * after the data type), it is natural for someone to come up with a way to declare a function pointer as follows:

int *f(double);

There's a catch here. That is actually a declaration of a function which accepts a double as an argument and returns a int*.

We need to explicitly tell the compiler that it is not a function returning a pointer; rather it is a pointer to a function. We do this by placing brackets (parenthesis has higher precedence) around *f.

int (*f)(double);

Let's take another example:

int* myfunc(int a[], char b);

To declare an array of function pointers which point to a function with the above signature, we write:

int* (*ptr[])(int[], char);

There is a trick known as Spiral Rule which is quite useful in finding out what a complicated data type is.


Using typedef

Things can get pretty complicated with function pointers. To keep the code readable and neat, we prefer giving a single symbol type name to function pointer types using typedef.

typedef int (*ptrFunc_t)(int, char);

This allows you to declare a pointer to a function which accepts an int and a char as arguments and returns an int as follows: ptrFunc_t myptr;

Community
  • 1
  • 1
Yashas
  • 1,154
  • 1
  • 12
  • 34
3

This:

void foo(const char*);

declares foo as a function that takes a const char* argument and returns void (i.e., it doesn't return anything). (It would have to be defined somewhere for you to be able to call it.)

This:

void (*bar)(const char*);

defines bar as a pointer object. It's a pointer to a function that has the same type as foo, above.

By adding the typedef keyword, this:

typedef void (*print_type)(const char*);

defined print_type, not as an object of that pointer-to-function type, but as an alias for that pointer-to-function type.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1

It is creating a typedef of a pointer-to-function, the function takes a const char pointer argument and retusn nothing.

Such typedefs are common in libraries that are meant for dynamic loading (that is with LoadLibrary etc), since you must load function pointers to the actual functions rather than being able to use the function name directly as you can with load-time linking.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23