Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Defining an array of pointer to function

Reply
Thread Tools

Defining an array of pointer to function

 
 
Rahul
Guest
Posts: n/a
 
      10-05-2010
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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      10-05-2010
* 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>
 
Reply With Quote
 
 
 
 
Rahul
Guest
Posts: n/a
 
      10-05-2010
On Oct 5, 5:00*pm, "Alf P. Steinbach /Usenet" <alf.p.steinbach
(E-Mail Removed)> 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) *(*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>


Thanks Alf!!

I got the funda.

 
Reply With Quote
 
Puppet_Sock
Guest
Posts: n/a
 
      10-05-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to array of array of const pointer RSL C++ 14 02-19-2010 02:06 PM
Array of pointer and pointer of array erfan C Programming 6 01-28-2008 08:55 PM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM



Advertisments