Interpreting some code

C

Chris Saunders

I came across a construction in some code I was reading that I have
never seen before and hope that someone could tell me how to
interpret this:

int (*literalScanners[XML_N_LITERAL_TYPES])(const ENCODING *,
const char *,
const char *,
const char **);

It looks like a function declaration to me but the "[XML_N_LITERAL_TYPES]"
part throws me off.

Regards
Chris Saunders
(e-mail address removed)
 
B

Ben Pfaff

Chris Saunders said:
int (*literalScanners[XML_N_LITERAL_TYPES])(const ENCODING *,
const char *,
const char *,
const char **);

Array of pointers to functions returning int.
 
J

Joona I Palaste

Chris Saunders said:
I came across a construction in some code I was reading that I have
never seen before and hope that someone could tell me how to
interpret this:
int (*literalScanners[XML_N_LITERAL_TYPES])(const ENCODING *,
const char *,
const char *,
const char **);
It looks like a function declaration to me but the "[XML_N_LITERAL_TYPES]"
part throws me off.

It's declaring an array of pointers to function, not declaring a
function. There are XML_N_LITERAL_TYPES pointers in that array, and
they each point to a function returning int and accepting parameters
(const ENCODING *, const char *, const char *, const char **).

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"I wish someone we knew would die so we could leave them flowers."
- A 6-year-old girl, upon seeing flowers in a cemetery
 
T

Tobias Oed

Chris said:
I came across a construction in some code I was reading that I have
never seen before and hope that someone could tell me how to
interpret this:

int (*literalScanners[XML_N_LITERAL_TYPES])(const ENCODING *,
const char *,
const char *,
const char **);

It looks like a function declaration to me but the "[XML_N_LITERAL_TYPES]"
part throws me off.

That's an array with XML_N_LITERAL_TYPES items of pointers to function
taking (const ENC...) as arguments and returning an int.
Tobias.
 
C

Chris Saunders

My thanks to all who responded.

Regards
Chris Saunders
(e-mail address removed)

Ben Pfaff said:
Chris Saunders said:
int (*literalScanners[XML_N_LITERAL_TYPES])(const ENCODING *,
const char *,
const char *,
const char **);

Array of pointers to functions returning int.
 
D

Dan Pop

In said:
I came across a construction in some code I was reading that I have
never seen before and hope that someone could tell me how to
interpret this:

int (*literalScanners[XML_N_LITERAL_TYPES])(const ENCODING *,
const char *,
const char *,
const char **);

It looks like a function declaration to me but the "[XML_N_LITERAL_TYPES]"
part throws me off.

Then remove it and you're left with:

int (*literalScanners)(const ENCODING *, const char * ...);

which can be easily recognised as the declaration of a pointer to
function. So, what do we have when we put "[XML_N_LITERAL_TYPES]" back
in? An array of pointers to functions.

Note that such declarations need not be that cryptical. A single typedef
can do wonders:

typedef int func_t(const ENCODING *, const char *, const char * ...);
func_t *literalScanners[XML_N_LITERAL_TYPES];

Now, it's glaringly obvious that literalScanners is an array of pointers.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top