Understanding a function declaration.

  • Thread starter Sri Harsha Dandibhotla
  • Start date
S

Sri Harsha Dandibhotla

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
 
C

Chris McDonald

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.


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
 
S

Sri Harsha Dandibhotla

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.

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.)
 
V

vippstar

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.
 
O

Old Wolf

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
 
S

Sri Harsha Dandibhotla

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*/
}
 
R

Richard

Richard Heathfield said:
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.
 
B

Bart

(e-mail address removed)...
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.
 
S

shantanu

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
 
J

John Bode

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
 
S

Sri Harsha Dandibhotla

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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top