4

I am new to C, and this typedef looks a little bit strange to me. Can someone explain what it does?

typedef void (*alpm_cb_log)(alpm_loglevel_t, const char *, va_list);

It is in a header file.

yasar
  • 13,158
  • 28
  • 95
  • 160

5 Answers5

7

You can use cdecl.org : http://cdecl.ridiculousfish.com/?q=void+%28*alpm_cb_log%29%28alpm_loglevel_t%2C+const+char+*%2C+va_list%29+

It says:

declare alpm_cb_log as pointer to function (alpm_loglevel_t, pointer to const char, va_list) returning void

in this case, it is a typedef, not a declaration.

dee-see
  • 23,668
  • 5
  • 58
  • 91
J-16 SDiZ
  • 26,473
  • 4
  • 65
  • 84
4

It defines alpm_cb_log to be a type for a pointer to a function that takes the arguments alpm_loglevel_t, const char *, va_list and returns void.

James M
  • 18,506
  • 3
  • 48
  • 56
4

A Simple example. Declaration:

typedef int myint.

Use:

myint number = 7;

myint is a synonym of int.

your example

typedef void (*alpm_cb_log)(alpm_loglevel_t, const char *, va_list);

this is a pointer to a function

(*alpm_cb_log)

The arguments are

(alpm_loglevel_t, const char *, va_list)

and does not return anything.

void 

The general rule with the use of typedef is to write out a declaration as if you were declaring variables of the types that you want

ugoren
  • 16,023
  • 3
  • 35
  • 65
user1138677
  • 155
  • 6
3

These do look weird if you've never seen them before. It's a typedef alpm_cb_log for a pointer to a function returning void, taking two or more arguments: an alpm_loglevel_t, a const char *, and a variable argument list.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
2

it creates the alais alpm_cb_log which is a pointer to a function that returns void and takes three paramaters. 1. a alpm_loglevel_t 2. const char *. 3 a varaibale argument list.

rerun
  • 25,014
  • 6
  • 48
  • 78