Defining an array of pointer to function

R

Rahul

Hi,

I want to write an array of pointer to function "which takes an array
of function pointer of type "int fun(int)" and returns a fptr of same
type

The following works well

typedef int (*fp)(int);
fp (*newFp[5]) (fp [5]);

But when I try to inline the typedefs then it gives me compilation
error in VC++ 2010 saying " syntax error : ')'"

int (*)(int) (*newFp[5]) ( int (*[5]) (int) )

How do I inline the declarations to remove the typedef (its not
required but am asking just out of curiosity)

Thanks in advance
Rahul
 
A

Alf P. Steinbach /Usenet

* Rahul, on 05.10.2010 11:38:
I want to write an array of pointer to function "which takes an array
of function pointer of type "int fun(int)" and returns a fptr of same
type

The following works well

typedef int (*fp)(int);
fp (*newFp[5]) (fp [5]);

But when I try to inline the typedefs then it gives me compilation
error in VC++ 2010 saying " syntax error : ')'"

int (*)(int) (*newFp[5]) ( int (*[5]) (int) )

How do I inline the declarations to remove the typedef (its not
required but am asking just out of curiosity)

Well it sounds like homework, but it's often asked and seldom answered.

Starting at the innermost thing, the name of the array:

newFp

What you can do with the array is to index it:

newFp[]

The result of that is a function pointer, which can be dereferenced:

*newFp[]

Yielding an untyped entity (corresponding to a delegate in other languages) that
can be called:

(*newFp[])(...)

The result of calling is a function pointer that can be dereferenced:

*(*newFp[])(...)

And called with int argument:

(*(*newFp[])(...))(int)

Yielding an int:

int (*(*newFp[])(...))(int)

The dots formal argument should be an array, and such array can be indexed:

int (*(*newFp[])(...[]))(int)

The result of indexing is a function pointer that can be dereferenced:

int (*(*newFp[])(...*...[]))(int)

And called with int argument:

int (*(*newFp[])(...(*[])(int) ))(int)

Yielding an int:

int (*(*newFp[])(int (*[])(int)))(int)

Given this it's understandable why both Brian Kernighan (I think it was) and
Bjarne Stroustrup have described the C declaration syntax as a "failed
experiment". :)

In short, use typedef.


Cheers & hth.,

- Alf
 
R

Rahul

* Rahul, on 05.10.2010 11:38:






I want to write an array of pointer to function "which takes an array
of function pointer of type "int fun(int)" and returns a fptr of same
type
The following works well
typedef int (*fp)(int);
fp (*newFp[5]) (fp [5]);
But when I try to inline the typedefs then it gives me compilation
error in VC++ 2010 saying " syntax error : ')'"
int (*)(int)  (*newFp[5])  ( int (*[5]) (int) )
How do I inline the declarations to remove the typedef (its not
required but am asking just out of curiosity)

Well it sounds like homework, but it's often asked and seldom answered.

Starting at the innermost thing, the name of the array:

   newFp

What you can do with the array is to index it:

   newFp[]

The result of that is a function pointer, which can be dereferenced:

   *newFp[]

Yielding an untyped entity (corresponding to a delegate in other languages) that
can be called:

   (*newFp[])(...)

The result of calling is a function pointer that can be dereferenced:

   *(*newFp[])(...)

And called with int argument:

   (*(*newFp[])(...))(int)

Yielding an int:

   int (*(*newFp[])(...))(int)

The dots formal argument should be an array, and such array can be indexed:

   int (*(*newFp[])(...[]))(int)

The result of indexing is a function pointer that can be dereferenced:

   int (*(*newFp[])(...*...[]))(int)

And called with int argument:

   int (*(*newFp[])(...(*[])(int) ))(int)

Yielding an int:

   int (*(*newFp[])(int (*[])(int)))(int)

Given this it's understandable why both Brian Kernighan (I think it was) and
Bjarne Stroustrup have described the C declaration syntax as a "failed
experiment". :)

In short, use typedef.

Cheers & hth.,

- Alf

Thanks Alf!!

I got the funda.
 
P

Puppet_Sock

On Oct 5, 8:00 am, "Alf P. Steinbach /Usenet"
[Alf explains a complicated declaration]

Whew! Yes indeed, thanks Alf. That was a ride.
Socks
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top