Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Function pointer array as parameter to a function

Reply
Thread Tools

Function pointer array as parameter to a function

 
 
aruna.mysore@gmail.com
Guest
Posts: n/a
 
      05-19-2008
Hi all,

I have a specific problem passing a function pointer array as a
parameter to a function. I am trying to use a function which takes a
function pointer array as an argument. I am too sure about the syntax
of calling the same.

#include <stdio.h>

void fp1()
{ printf("In fp1\n"); }

void fp2()
{ printf("In fp2\n");}

void fp3()
{ printf("In fp3\n");}

void call_fpn(void (*fp[3])())
{
(*fp[0])();
(*fp[1])();
(*fp[2])();
}

void main()
{
void (*pfn[3])()={NULL};
pfn[0]=&fp1;
pfn[1]=&fp2;
pfn[2]=&fp3;

call_fpn(.....);
}

Can someone please let me know what is the right syntax for calling
the function accepting a function pointer array.

Thanks in advance.
Ar
 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      05-19-2008
On May 19, 4:18 pm, (E-Mail Removed) wrote:
> Hi all,
>
> I have a specific problem passing a function pointer array as a
> parameter to a function. I am trying to use a function which takes a
> function pointer array as an argument. I am too sure about the syntax
> of calling the same.

<snip c code>
> Can someone please let me know what is the right syntax for calling
> the function accepting a function pointer array.



#include <stdio.h>

#define foo() printf("%s\n", __func__)

void f1() { foo(); }
void f2() { foo(); }
void f3() { foo(); }
void doit(void (**f)()) {
size_t i;

for(i = 0; f[i]; f[i++]())
;
}

int main(void) {

void (*f[4])() = {0};

f[0] = f1;
f[1] = f2;
f[2] = f3;

doit(f);

return 0;
}
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      05-19-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have a specific problem passing a function pointer array as a
> parameter to a function. I am trying to use a function which takes a
> function pointer array as an argument. I am too sure about the syntax
> of calling the same.


You're attempt already looks rather good.

> #include <stdio.h>


> void fp1()


I think it's better to exactly specify the the types of arguments
(or that no argument is to be expected):

void fp1( void )

> { printf("In fp1\n"); }


> void fp2()
> { printf("In fp2\n");}


> void fp3()
> { printf("In fp3\n");}


> void call_fpn(void (*fp[3])())
> {
> (*fp[0])();
> (*fp[1])();
> (*fp[2])();
> }


You can simplify that to

void call_fpn( void ( * fp[ 3 ] )( void ) )
{
fp[ 0 ]( );
fp[ 1 ]( );
fp[ 2 ]( );
}

A function pointer followed by parentheses does call the function,
no need to dereference the pointer.

And if you don't want to restrict yourself to an array of
fixed size just use

void call_fpn( void ( ** fp )( void ) )

> void main()


main() always returns an int, so make that

int main( void )

> {
> void (*pfn[3])()={NULL};


You can do the initialization already with the definition:

void ( * pfn[ 3 ] )( void ) = { fp1, fp2, fp3 };

> pfn[0]=&fp1;


The name of the function alone is already a pointer to the
function (at least when it's used as a value), so the '&'
isn't necessary.

> pfn[1]=&fp2;
> pfn[2]=&fp3;


> call_fpn(.....);


Just pass 'pfn' as the argument:

call_fpn( pfn );

return 0;
> }

Regards, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      05-19-2008
On May 19, 4:50 pm, (E-Mail Removed) (Jens Thoms Toerring) wrote:
> (E-Mail Removed) wrote:
> > I have a specific problem passing a function pointer array as a
> > parameter to a function. I am trying to use a function which takes a
> > function pointer array as an argument. I am too sure about the syntax
> > of calling the same.

>
> You're attempt already looks rather good.
>
> > #include <stdio.h>
> > void fp1()

>
> I think it's better to exactly specify the the types of arguments
> (or that no argument is to be expected):
>
> void fp1( void )
>
> > { printf("In fp1\n"); }
> > void fp2()
> > { printf("In fp2\n");}
> > void fp3()
> > { printf("In fp3\n");}
> > void call_fpn(void (*fp[3])())
> > {
> > (*fp[0])();
> > (*fp[1])();
> > (*fp[2])();
> > }

>
> You can simplify that to
>
> void call_fpn( void ( * fp[ 3 ] )( void ) )
> {
> fp[ 0 ]( );
> fp[ 1 ]( );
> fp[ 2 ]( );
>
> }
>
> A function pointer followed by parentheses does call the function,
> no need to dereference the pointer.
>
> And if you don't want to restrict yourself to an array of
> fixed size just use

He isn't. The [3] really is just informative, but actually a pointer.
<snip>
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      05-19-2008
(E-Mail Removed) wrote:
> On May 19, 4:50 pm, (E-Mail Removed) (Jens Thoms Toerring) wrote:
> >
> > void call_fpn( void ( * fp[ 3 ] )( void ) )
> >
> > And if you don't want to restrict yourself to an array of
> > fixed size just use

> He isn't. The [3] really is just informative, but actually a pointer.


Of course, you're right.
Regards, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Bart
Guest
Posts: n/a
 
      05-19-2008
On May 19, 2:50*pm, (E-Mail Removed) (Jens Thoms Toerring) wrote:
> (E-Mail Removed) wrote:
>
> You can simplify that to


> void call_fpn( void ( * fp[ 3 ] )( void ) )


You seem to be quite good at this:

Consider that parameter type, call it T:

void (*fp[3])void

How could I modify T to end up with an array [N] of T?

How could I modify T to have a pointer to T?

How could I modify T to have a function returning type T?

Please add any parentheses that might be needed.

(I need ask a similar question in a different guise recently. But no
replies, so it was either incredibly difficult, or so trivial that
nobody could understand why I was even asking. I contrived a solution
of sorts but it was unsatisfactory.)

-- Thanks,

Bartc
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      05-19-2008
Bart <(E-Mail Removed)> wrote:
> On May 19, 2:50*pm, (E-Mail Removed) (Jens Thoms Toerring) wrote:
> > (E-Mail Removed) wrote:
> >
> > You can simplify that to


> > void call_fpn( void ( * fp[ 3 ] )( void ) )


> Consider that parameter type, call it T:


> void (*fp[3])void


I guess you meant

void (*fp[3])( void )

> How could I modify T to end up with an array [N] of T?


> How could I modify T to have a pointer to T?


> How could I modify T to have a function returning type T?


I am not sure if I understand your questions correctly, so
what I write in the following might be completely off the
mark...

First thing I would consider when things get too complicated
is using a typedef for the type of function pointer passed
around, e.g.

typedef void ( * func_t )( void );

'func_t' is now a new type that is a pointer to a function
which takes no arguments and returns nothing.

Now you can e.g. write the call_fpn() function as

void call_fpn( func_t fp[ 3 ] )

which already looks a lot more readable.

As someone else already has pointed out the '[3]' bit isn't
really relevant, what the function receives is a pointer to
the first element of an array of function pointers. So you
could also write that as

void call_fpn( func_t fp[ ] )

or

void call_fpn( func_t *fp )

If you now want the call_fpn() function to return a function
pointer of the same type as the ones in the array it got
passed then just change it to e.g.

func_t call_fpn( func_t *fp )
{
fp[ 0 ]( );
fp[ 1 ]( );
return fp[ 2 ];
}

Then you can do in main() e.g.

int main( void )
{
funct_t pfn[ 3 ] = { fp1, fp2, fp3 }
call_fpn( pfn )( );
return 0;
}

This will result in fp1() and fp2() getting called from within
call_fpn() and then the function returned by call_fpn(), fp3,
being called from within main().

If you insist on not using a typedef things will look a lot
uglier. E.g. call_fpn() then would have to be defined instead
of

func_t call_fpn( func_t *fp )

as

void ( * call_fpn( void ( ** fp )( void ) ) )( void )

(I hope I got that right, at least the compiler doesn't com-
plain which I find extremely hard to understand...

Does that about answer what you asked? Otherwise please help
me by trying to describe the problem a bit differently.

Regards, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-19-2008
(E-Mail Removed) writes:
> I have a specific problem passing a function pointer array as a
> parameter to a function. I am trying to use a function which takes a
> function pointer array as an argument. I am too sure about the syntax
> of calling the same.

[...]

You can't. In C, a function cannot take an array as a parameter.

It can *appear* to do so:

void func(int arr[]);

...

int my_array[42];
func(my_array);

but in fact the parameter "arr" is just a pointer, and the argument
"my_array" is implicitly *converted* to a pointer (to the first
element of the array) when it's evaluated.

See section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
aruna.mysore@gmail.com
Guest
Posts: n/a
 
      05-19-2008
Hi all,

Thank you everyone for your time and responses.

Thanks and Regards,
Ar
 
Reply With Quote
 
Bart
Guest
Posts: n/a
 
      05-19-2008
On May 19, 4:58*pm, (E-Mail Removed) (Jens Thoms Toerring) wrote:
> Bart <(E-Mail Removed)> wrote:
> > On May 19, 2:50*pm, (E-Mail Removed) (Jens Thoms Toerring) wrote:
> > > (E-Mail Removed) wrote:

>
> > > You can simplify that to
> > > void call_fpn( void ( * fp[ 3 ] )( void ) )

> > Consider that parameter type, call it T:
> > void (*fp[3])void

>
> I guess you meant
>
> void (*fp[3])( void )


Yes.

>
> > How could I modify T to end up with an array [N] of T?
> > How could I modify T to have a pointer to T?
> > How could I modify T to have a function returning type T?

>
> I am not sure if I understand your questions correctly, so
> what I write in the following might be completely off the
> mark...

...
> void ( * call_fpn( void ( ** fp )( void ) ) )( void )
>
> (I hope I got that right, at least the compiler doesn't com-
> plain which I find extremely hard to understand...
>
> Does that about answer what you asked? Otherwise please help
> me by trying to describe the problem a bit differently.


I find C types incomprehensible other than the simplest.

So I was after a way of simply building up a complex type step by
step, working from an left-to-right description in English. And
starting from a non-trivial type so that I can see how you place any
new stuff in relation to *, [] and ().

Typedefs are also a useful build tool but I think I need to be able
build types out of primary elements first. This is for both straight
coding, and machine-generated code when readability isn't so
important.

--
Bartc
 
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
Cast a pointer to array to base class pointer to array Hansen C++ 3 04-24-2010 03:30 PM
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



Advertisments