By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,934 Members | 1,804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,934 IT Pros & Developers. It's quick & easy.

Understanding a function declaration.

P: n/a
Hello all.
I recently came across a function declaration as :
char(*(*x())[ ])();
This was not in some code but it was in a C questions thread on some
group.
I tried to decipher what it returns but couldn't make complete sense
out of it. Can someone please explain what this function is supposed
to return and expand it step by step.

Thanks,
Harsha
Jun 27 '08 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Sri Harsha Dandibhotla <ha********@gmail.comwrites:
>Hello all.
I recently came across a function declaration as :
char(*(*x())[ ])();
This was not in some code but it was in a C questions thread on some
group.
I tried to decipher what it returns but couldn't make complete sense
out of it. Can someone please explain what this function is supposed
to return and expand it step by step.

Hello Harsha,

You may like to seek out a helpful tool named 'cdecl',
which is available as C source and, thus, runs on a variety of platforms.

For your example it gives the answer:

SHELL$ cdecl
Type `help' or `?' for help
cdecl explain char(*(*x())[ ])()
declare x as function returning pointer to array of pointer to function returning char
--
Chris.

Jun 27 '08 #2

P: n/a
Sri Harsha Dandibhotla said:
Hello all.
I recently came across a function declaration as :
char(*(*x())[ ])();
This was not in some code but it was in a C questions thread on some
group.
I tried to decipher what it returns but couldn't make complete sense
out of it. Can someone please explain what this function is supposed
to return and expand it step by step.
Start with x, the identifier.

Look immediately right. Is it a [ ? No.

Is it a (? Yes: x( Keep reading until you hit the closing parenthesis.

char(*(* )[ ])()
x()
"x is a function returning"

Now read left. Is it a ( ? No.

Is it a *, const, or volatile? If so, keep reading left till it isn't:

char(*( )[ ])()
*x()

"x is a function returning a pointer to"

Is the next thing on the left a (? Yes, and this is a grouping thing, so we
can just balance out parentheses, without adding anything:

char(* [ ])()
(*x())

"x is a function returning a pointer to"

Look right. Is the thing on the right a [? Yes, it is.

char(* )()
(*x())[ ]

"x is a function returning a pointer to an array of"

Is the thing to the right a (? No.

Is the thing to the left a (? No.

Is the thing to the left a const, volatile, or *? Yes. Keep reading left until
it isn't.

char( )()
*(*x())[ ]

"x is a function returning a pointer to an array of pointer to"

Is the thing to the left a (? Yes. Read up to the ).

char ()
(*(*x())[ ])

"x is a function returning a pointer to an array of pointer to"

Is the thing to the right a [ ? No.
Is the thing to the right a (? Yes. Read up to the matching )...

char
(*(*x())[ ])()
"x is a function returning a pointer to an array of pointer to function
returning"

Is the thing to the left a (? No.
Is it const/volatile/*? No.
Whatever's left, tack on the end:

char (*(*x())[ ])()
"x is a function returning a pointer to an array of pointer to function
returning char"

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #3

P: n/a
On Jun 16, 11:36 am, Chris McDonald <ch...@csse.uwa.edu.auwrote:
Sri Harsha Dandibhotla <harsha....@gmail.comwrites:
Hello all.
I recently came across a function declaration as :
char(*(*x())[ ])();
This was not in some code but it was in a C questions thread on some
group.
I tried to decipher what it returns but couldn't make complete sense
out of it. Can someone please explain what this function is supposed
to return and expand it step by step.

Hello Harsha,

You may like to seek out a helpful tool named 'cdecl',
which is available as C source and, thus, runs on a variety of platforms.

For your example it gives the answer:

SHELL$ cdecl
Type `help' or `?' for help
cdecl explain char(*(*x())[ ])()
declare x as function returning pointer to array of pointer to function returning char
Thank you.
But, the tool gives the final translation, doesn't it.
Is there any other tool or is there a command line option by which it
outputs the translation as it parses?
Example: Mr.Heathfield's translation(although not as detailed.)

Jun 27 '08 #4

P: n/a
On Jun 16, 10:04 am, Sri Harsha Dandibhotla <harsha....@gmail.com>
wrote:
On Jun 16, 11:36 am, Chris McDonald <ch...@csse.uwa.edu.auwrote:
Sri Harsha Dandibhotla <harsha....@gmail.comwrites:
>Hello all.
>I recently came across a function declaration as :
>char(*(*x())[ ])();
>This was not in some code but it was in a C questions thread on some
>group.
>I tried to decipher what it returns but couldn't make complete sense
>out of it. Can someone please explain what this function is supposed
>to return and expand it step by step.
Hello Harsha,
You may like to seek out a helpful tool named 'cdecl',
which is available as C source and, thus, runs on a variety of platforms.
For your example it gives the answer:
SHELL$ cdecl
Type `help' or `?' for help
cdecl explain char(*(*x())[ ])()
declare x as function returning pointer to array of pointer to function returning char

Thank you.
But, the tool gives the final translation, doesn't it.
Is there any other tool or is there a command line option by which it
outputs the translation as it parses?
Example: Mr.Heathfield's translation(although not as detailed.)
I think it's best to avoid such tools and instead learning to read the
types as Mr Heathfield has demonstrated.
Jun 27 '08 #5

P: n/a
On Jun 16, 6:24 pm, Sri Harsha Dandibhotla <harsha....@gmail.com>
wrote:
Hello all.
I recently came across a function declaration as :
char(*(*x())[ ])();
(*(*x())[]) is a function returning char

therefore
(*x())[] is a pointer to function returning char

therefore
*x() is an array of pointers to function returning char

therefore
x() is a pointer to an array of pointers to function returning char

therefore
x is a function returning a pointer to an array of
pointers to function returning char
Jun 27 '08 #6

P: n/a
On Jun 16, 11:52 am, Richard Heathfield <r...@see.sig.invalidwrote:
Sri Harsha Dandibhotla said:
Hello all.
I recently came across a function declaration as :
char(*(*x())[ ])();
This was not in some code but it was in a C questions thread on some
group.
I tried to decipher what it returns but couldn't make complete sense
out of it. Can someone please explain what this function is supposed
to return and expand it step by step.

Start with x, the identifier.

Look immediately right. Is it a [ ? No.

Is it a (? Yes: x( Keep reading until you hit the closing parenthesis.

char(*(* )[ ])()
x()
"x is a function returning"
<rest snipped>

This is where I have trouble understanding. Do the parentheses on the
outside accept arguments are do those on the inside beside x.
that is, if I have to define the function, will it be

char(*(*x())[ ])(/*arguments to function*/)
{
/*function body*/
}

or will it be
char(*(*x(/*arguments to function*/))[ ])()
{
/*function body*/
}
Jun 27 '08 #7

P: n/a
Sri Harsha Dandibhotla said:
On Jun 16, 11:52 am, Richard Heathfield <r...@see.sig.invalidwrote:
>Sri Harsha Dandibhotla said:
Hello all.
I recently came across a function declaration as :
char(*(*x())[ ])();
This was not in some code but it was in a C questions thread on some
group.
I tried to decipher what it returns but couldn't make complete sense
out of it. Can someone please explain what this function is supposed
to return and expand it step by step.

Start with x, the identifier.

Look immediately right. Is it a [ ? No.

Is it a (? Yes: x( Keep reading until you hit the closing parenthesis.

char(*(* )[ ])()
x()
"x is a function returning"
<rest snipped>

This is where I have trouble understanding. Do the parentheses on the
outside accept arguments are do those on the inside beside x.
Either can, and neither must.
that is, if I have to define the function, will it be

char(*(*x())[ ])(/*arguments to function*/)
{
/*function body*/
}

or will it be
char(*(*x(/*arguments to function*/))[ ])()
{
/*function body*/
}
typedef is your friend.

We have a function returning a pointer to an array of pointer to function
returning char. Let's start off by defining a type, "function returning char".
I invent the convention that ft_ means "function type", and it is followed by
a printf-modifier-style abbrev representing the return type and, optionally,
something along the same lines for parameters:

typedef char ft_c(); /* ft_c is a synonym for the type "function returning
char" */

Now our declaration is reduced to:

ft_c *(*x())[];

which we might read as: "x is a function returning a pointer to an array of
pointer to ft_c", and the answer to your question now becomes clear. If we
wish for x() to take parameters, they go in the innermost parentheses, the
ones right after the identifier:

ft_c *(*x(double, char *))[];

"x is a function taking a double and a char *, which returns a pointer to an
array of pointer to ft_c"

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #8

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrites:
Sri Harsha Dandibhotla said:
>Hello all.
I recently came across a function declaration as :
char(*(*x())[ ])();
This was not in some code but it was in a C questions thread on some
group.
I tried to decipher what it returns but couldn't make complete sense
out of it. Can someone please explain what this function is supposed
to return and expand it step by step.

Start with x, the identifier.

Look immediately right. Is it a [ ? No.

Is it a (? Yes: x( Keep reading until you hit the closing parenthesis.

char(*(* )[ ])()
x()
"x is a function returning"

Now read left. Is it a ( ? No.

Is it a *, const, or volatile? If so, keep reading left till it isn't:

char(*( )[ ])()
*x()

"x is a function returning a pointer to"

Is the next thing on the left a (? Yes, and this is a grouping thing, so we
can just balance out parentheses, without adding anything:

char(* [ ])()
(*x())

"x is a function returning a pointer to"

Look right. Is the thing on the right a [? Yes, it is.

char(* )()
(*x())[ ]

"x is a function returning a pointer to an array of"

Is the thing to the right a (? No.

Is the thing to the left a (? No.

Is the thing to the left a const, volatile, or *? Yes. Keep reading left until
it isn't.

char( )()
*(*x())[ ]

"x is a function returning a pointer to an array of pointer to"

Is the thing to the left a (? Yes. Read up to the ).

char ()
(*(*x())[ ])

"x is a function returning a pointer to an array of pointer to"

Is the thing to the right a [ ? No.
Is the thing to the right a (? Yes. Read up to the matching )...

char
(*(*x())[ ])()
"x is a function returning a pointer to an array of pointer to function
returning"

Is the thing to the left a (? No.
Is it const/volatile/*? No.
Whatever's left, tack on the end:

char (*(*x())[ ])()
"x is a function returning a pointer to an array of pointer to function
returning char"
I must say, well explained - but highlights why I would never ask a
question like this in an interview. It's nigh on impossible to do it in
the head. Well, in my booze addled one anyway.

Jun 27 '08 #9

P: n/a

"Sri Harsha Dandibhotla" <ha********@gmail.comwrote in message
news:bc561c31-
c2*************************@l64g2000...legrou ps.com...
Hello all.
I recently came across a function declaration as :
char(*(*x())[ ])();
This was not in some code but it was in a C questions thread on some
group.
I tried to decipher what it returns but couldn't make complete sense
out of it. Can someone please explain what this function is supposed
to return and expand it step by step.
I once proposed here a linear, left-to-right notation for C
declarations:

Your example would have come out as (using 'function' for extra
clarity):

function() * [] * function() char x

Read as "function returning pointer to array of pointer to function
returning char, x"

No complicated algorithm, you just need to be able to read.

But I don't remember there was much approval. Perhaps once somebody
does master the declarations they don't want their efforts undermined!
Aside from it being rather difficult to incorporate into C at this
stage.

Fortunately examples such as yours aren't common.

--
Bartc
Jun 27 '08 #10

P: n/a
On Jun 16, 11:24*am, Sri Harsha Dandibhotla <harsha....@gmail.com>
wrote:
Hello all.
I recently came across a function declaration as :
char(*(*x())[ ])();
This was not in some code but it was in a C questions thread on some
group.
I tried to decipher what it returns but couldn't make complete sense
out of it. Can someone please explain what this function is supposed
to return and expand it step by step.

Thanks,
Harsha
Hi Harsha,

if start with x() and move outwards it makes the things very simple.
the above mentioned statement reads x is a function which return a
pointer to an array of pointer to function which returns a character.

Shantanu.
Shantanu
Jun 27 '08 #11

P: n/a
On Jun 16, 1:24 am, Sri Harsha Dandibhotla <harsha....@gmail.com>
wrote:
Hello all.
I recently came across a function declaration as :
char(*(*x())[ ])();
This was not in some code but it was in a C questions thread on some
group.
I tried to decipher what it returns but couldn't make complete sense
out of it. Can someone please explain what this function is supposed
to return and expand it step by step.

Thanks,
Harsha
Find the leftmost identifier, then work your way out, remembering that
[] and () bind before *:

x -- x
x() -- is a function
*x() -- returning a pointer
(*x())[] -- to an array
*(*x())[] -- of pointers
(*(*x())[])() -- to functions
char (*(*x())[])() -- returning char
Jun 27 '08 #12

P: n/a
On Jun 16, 9:31 pm, John Bode <john_b...@my-deja.comwrote:
On Jun 16, 1:24 am, Sri Harsha Dandibhotla <harsha....@gmail.com>
wrote:
Hello all.
I recently came across a function declaration as :
char(*(*x())[ ])();
This was not in some code but it was in a C questions thread on some
group.
I tried to decipher what it returns but couldn't make complete sense
out of it. Can someone please explain what this function is supposed
to return and expand it step by step.
Thanks,
Harsha

Find the leftmost identifier, then work your way out, remembering that
[] and () bind before *:

x -- x
x() -- is a function
*x() -- returning a pointer
(*x())[] -- to an array
*(*x())[] -- of pointers
(*(*x())[])() -- to functions
char (*(*x())[])() -- returning ch
I thank you all for the responses. My only confusion was with whether
the inner parentheses determine the arguments to be passed or the
outer ones.
It has been clarified.
Jun 27 '08 #13

P: n/a
Sri Harsha Dandibhotla wrote:
>
I recently came across a function declaration as :
char(*(*x())[ ])();
This was not in some code but it was in a C questions thread on
some group. I tried to decipher what it returns but couldn't
make complete sense out of it. Can someone please explain what
this function is supposed to return and expand it step by step.
[1] c:\djgpp\include>cdecl
Type `help' or `?' for help
cdeclexplain char(*(*x())[ ])()
declare x as function returning pointer to array of pointer to
function returning char

(last is one line)

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #14

This discussion thread is closed

Replies have been disabled for this discussion.