|
|
Defining an array of pointer to function
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
|
|
0
|
|
|
|
Reply
|
rsharma.champ (12)
|
10/5/2010 9:38:11 AM |
|
* 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
--
blog at <url: http://alfps.wordpress.com>
|
|
0
|
|
|
|
Reply
|
Alf
|
10/5/2010 12:00:25 PM
|
|
On Oct 5, 5:00=A0pm, "Alf P. Steinbach /Usenet" <alf.p.steinbach
+use...@gmail.com> wrote:
> * 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) =A0(*newFp[5]) =A0( 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:
>
> =A0 =A0newFp
>
> What you can do with the array is to index it:
>
> =A0 =A0newFp[]
>
> The result of that is a function pointer, which can be dereferenced:
>
> =A0 =A0*newFp[]
>
> Yielding an untyped entity (corresponding to a delegate in other language=
s) that
> can be called:
>
> =A0 =A0(*newFp[])(...)
>
> The result of calling is a function pointer that can be dereferenced:
>
> =A0 =A0*(*newFp[])(...)
>
> And called with int argument:
>
> =A0 =A0(*(*newFp[])(...))(int)
>
> Yielding an int:
>
> =A0 =A0int (*(*newFp[])(...))(int)
>
> The dots formal argument should be an array, and such array can be indexe=
d:
>
> =A0 =A0int (*(*newFp[])(...[]))(int)
>
> The result of indexing is a function pointer that can be dereferenced:
>
> =A0 =A0int (*(*newFp[])(...*...[]))(int)
>
> And called with int argument:
>
> =A0 =A0int (*(*newFp[])(...(*[])(int) ))(int)
>
> Yielding an int:
>
> =A0 =A0int (*(*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
>
> --
> blog at <url:http://alfps.wordpress.com>
Thanks Alf!!
I got the funda.
|
|
0
|
|
|
|
Reply
|
rsharma.champ (12)
|
10/5/2010 4:01:22 PM
|
|
On Oct 5, 8:00=A0am, "Alf P. Steinbach /Usenet"
[Alf explains a complicated declaration]
Whew! Yes indeed, thanks Alf. That was a ride.
Socks
|
|
0
|
|
|
|
Reply
|
Puppet_Sock
|
10/5/2010 5:11:51 PM
|
|
|
3 Replies
73 Views
(page loaded in 0.071 seconds)
|
|
|
|
|
|
|
|
|