473,387 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Understanding a function declaration.

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
13 1934
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
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
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
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
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
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
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
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

"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: The Master | last post by:
Ok I went through and deleted code until I discovered the culprit. I then went back to my original style sheet and removed the CSS declaration that caused the floating to brake in safari 1.2. Once...
21
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am...
3
by: Dennis Chang | last post by:
Hi all, I was reading about function pointers and came across something which intrigued me. K&R2 calls qsort (pg.119) within main as so: qsort( (void **) lineptr, 0, nlines-1, (int (*) (void...
6
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the...
20
by: Christian Christmann | last post by:
Hi, in a benchmark I've found an uncommon use of a function. This is the simplified form: 1 int foo( int f ) 2 { 3 return f; 4 } 5
20
by: svata | last post by:
Hello there, after some time of pondering I come to some solution which would suit me best. Please correct, if I am wrong. Function has two parameters. A string array, better said a pointer to...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
5
by: Redmond | last post by:
Celerity is a powerful application for analysing C/C++ files. * It can process millions of source code lines. It supports standard C/C++. For each project, it shows the source files, include...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.