-2
typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);

As far as I know, typedef assigns new name to types in standard library.In this situation an instance of *http_data_cb is an int, but how about (http_parser*, const char *at, size_t length)?

Here is the link of the whole code

Thanks,

Community
  • 1
  • 1
smttsp
  • 4,011
  • 3
  • 33
  • 62

3 Answers3

2

It is a way do typedef a type that is a pointer to function. Typically in such typedefs you would not name the arguments to the function, but only indicate their type.

So a variable of the type http_data_cb will be a pointer to a function returning int result and taking three arguments of type http_parser*, const char and size_t in this order.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • 1
    I won't say that you typically not name the arguments as for documentation reasons it's often valuable to have names for the arguments. Of course they are optional but in my opinion highly recommended. – junix Jan 30 '13 at 12:50
  • `http_data_cb(InstanceOfParser, string_ptr, string_len);` is going to return me an int, right? – smttsp Jan 30 '13 at 12:52
  • @virtuecan in fact you do something of the sort of `http_data_cb f=;(*f)(InstanceOfParser, string_ptr, string_len);` – Ivaylo Strandjev Jan 30 '13 at 12:55
1

It declares a function pointer type.

The type-alias http_data_cb is a pointer to a function that receives three arguments and returns an integer.

You can use that to have pointers to other functions, for example to pass as callbacks in an event-driven system.

For more help reading and understanding declarations, see e.g. the clockwise/spiral rule.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • `http_data_cb(InstanceOfParser, string_ptr, string_len);` is going to return me an int, right? – smttsp Jan 30 '13 at 12:52
  • @virtuecan Yes, if you declare a variable of type `http_data_cb` and make it point to a functin with the correct signature, then you can use your variable (not the type) as a function and it should return an integer. – Some programmer dude Jan 30 '13 at 12:56
  • Thanks for help, if I had enough reputation would vote your comment up :( – smttsp Jan 30 '13 at 12:57
0

http_data_cb : is a variable

(*http_data_cb): is a pointer variable

(*http_data_cb)(...): is a pointer variable to a function

(*http_data_cb)(http_parser*, const char *at, size_t length): is a pointer variable to a function that recieves (http_parser*, const char *at, size_t length)

int (*http_data_cb)(http_parser*, const char *at, size_t length): is a pointer variable to a function that recieves (http_parser*, const char *at, size_t length) and returns an int.

typedef int (*http_data_cb)(http_parser*, const char *at, size_t length): http_data_cb is declared to be a new datatype alias as a pointer variable to a function that recieves (http_parser*, const char *at, size_t length) and returns an int.

Ghasan غسان
  • 5,577
  • 4
  • 33
  • 44