On 31.08.21 12:33,
hongy...@gmail.com wrote:
> See the following c code snippet located at here [1]:
>
> typedef struct protocol {
> const int default_port;
> int(*const parse_packet)(const char *, size_t, char **);
> } protocol_t;
>
> In the above definition, it seems to me that the `int ...' line is difficult to understand. So I want to dig/find some clues from the extensive/exhaustive *nix man page. I'm not sure if there is a convenient way to do this.
I'm not sure if the manual pages will be of any help here, but analyzing
this is quite simple:
1) As there is a type designator ("int") at the beginning and a list of
(what looks like) parameters at the end, this looks like a function
declaration. So this is a function expecting three arguments of the
proper kind and returning an int.
2) Where there is usually a function *name*, there is a "*const
parse_packet" (put between parentheses to satisfy C's precendece rules),
so (leaving the "const" aside for a moment), "* parse_packet" is the
function "name", so "parse_packet" is the name of a pointer to the function.
3) The "const" in front of the pointer name means that this field is
"constant", so, once initialialized, cannot be changed any more.
Maybe check
http://unixwiz.net/techtips/reading-cdecl.html
Josef
I can vaguely recall having seen a program which takes an arbitrarily
complex variable declaration and spits out a (human readable)
description of the declaration, eg
int *a; -> "a pointer to an int"
But I can't find it atm.