This was somewhere lying around in one my old HDD backups, and I always wanted to put it somewhere convenient for me to be able to reference it on demand.

It’s a post from comp.lang.c++ from when I was doing a lot of C++ development (most of which I’ve forgotten now that I use C# and .NET for EPLAN API development), but I think it’s just too good to let go to waste.

For those who might be interested in such things, enjoy:

Re: int (*t(int))(int *); // what does this mean?

Author: jim.hyslop
Email: jim.hyslop@leitch.com
Date: 1998/09/17
Forums: comp.lang.c++.moderated, comp.lang.c++
————————————————————————

In article <6tok7l$3ook$1@newssvr04-int.news.prodigy.com>,
“JER” wrote:
> Please give me the meaning of the declaration:
> int ( * t (int) )( int * );

The rules for parsing are:
Start at the identifier. Process to the right until you hit the end of the declaration or a closing parenthesis. Process to the left until you
hit an opening parenthesis or the beginning of the declaration. Repeat until the declaration is fully parsed.

So, the declaration is read as:
int ( * t (int) )( int * ) ;
10 6 5 1 2 3 4 7 8 9 11

1 – t
2 – is a function
3 – which accepts an int parameter
4 – [closing parenthesis – parse backwards now]
5 – and returns a pointer
6 – [opening parenthesis – parse forwards now]
7 – to a function
8 – which accepts an int *
9 – [closing parenthesis – parse backwards now]
10- and returns an int [beginning of dec – parse forwards now]
11- end of declaration – you’re done.

So: “t is a function which accepts an int parameter and returns a
pointer to a
function which accepts an int * and returns an integer”. The following
code
will work with your declaration:

#include 
using std::cout;
using std::endl;

int x(int *val)
{
  cout << "In x, passed value of " << *val << endl;
  return *val;
}

int y(int *val)
{
  cout << "In y, passed value of " << *val << endl;
  return *val;
}

int z(int *val)
{
  cout << "In z, passed value of " << *val << endl;
  return *val;
}

int ( * t (int v) )( int *) // Your original declaration
{
   // Exercise to the reader - how do you parse arr:
   // Hint: "=" is the end-of-declaration
   static int (*arr[])(int *) = {x, y, z};

   if (v < 0 || v >= sizeof(arr) / sizeof(arr[0])) {
      return NULL;
   }
   return arr[v];
}

int main(void)
{
  // and how do you parse:
  int(*p) (int *);

  int count=0;
  p=t(count);
  while(p) {
     cout << "p(&count) returned " << p(&count) << endl;
     p=t(++count);
  }
}