473,836 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function pointer array as parameter to a function

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
Jun 27 '08 #1
26 4894
ru*********@gma il.com 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.

#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.
Isn't

call_fpn(pfn);

enough?
>
Thanks in advance.
Ar

--
Pietro Cerutti
Jun 27 '08 #2
On May 19, 4:18 pm, aruna.mys...@gm ail.com 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;
}
Jun 27 '08 #3
ar**********@gm ail.com 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 ___ jt@toerring.de
\______________ ____________ http://toerring.de
Jun 27 '08 #4
On May 19, 4:50 pm, j...@toerring.d e (Jens Thoms Toerring) wrote:
aruna.mys...@gm ail.com 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>
Jun 27 '08 #5
vi******@gmail. com wrote:
On May 19, 4:50 pm, j...@toerring.d e (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 ___ jt@toerring.de
\______________ ____________ http://toerring.de
Jun 27 '08 #6
On May 19, 2:50*pm, j...@toerring.d e (Jens Thoms Toerring) wrote:
aruna.mys...@gm ail.com 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
Jun 27 '08 #7
Bart <bc@freeuk.comw rote:
On May 19, 2:50Â*pm, j...@toerring.d e (Jens Thoms Toerring) wrote:
aruna.mys...@gm ail.com 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 ___ jt@toerring.de
\______________ ____________ http://toerring.de
Jun 27 '08 #8
ar**********@gm ail.com 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_Keit h) ks***@mib.org <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"
Jun 27 '08 #9
Hi all,

Thank you everyone for your time and responses.

Thanks and Regards,
Ar
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
10198
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
8
4123
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
14
2497
by: dam_fool_2003 | last post by:
Hai, When I declare a zero-based array I get a compile time error stating that zero based array is not possible. But if I declare the same in the function argument I don't get any error. Is related with the function in C in which the argument's references are passed in the stack during the runtime. I am confused.
4
157999
by: Isaac | last post by:
Hi mates I want to know a simple program of return array from function ? Do I need to use pointer to return the address of the first element in an array. Isaac
10
10039
by: Barbrawl McBribe | last post by:
Is is possible to use typedefs to cast function pointers? I think I saw this in the WINGs src; grep for '(hashFunc)'. So far, trying to use a typedef to cast function pointers so that a return value of float is changed to float seems not to work; I belive it was like this: typedef int (*foo) (int); function_ptr_bar = (foo)function_that_returns_float;
15
2250
by: Daniel Rudy | last post by:
Hello, Consider the following code: /* resolve_hostname this resolves the hostname into an ip address. */ static void resolve_hostname(char result, const char hostname, const char server) {
17
3271
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
13
2029
by: Justcallmedrago | last post by:
How would you declare and assign a variable inside a function THAT HAS THE NAME OF A PARAMETER YOU PASSED example: when you call createvariable("myvariable") it will declare the variable "myvariable" and then maybe assign it something. myvariable = "this is a real variable"
5
2081
by: Fei Liu | last post by:
Hello, I just hit a strange problem regarding SFINAE. The following code causes compile error (void cannot be array element type), I thought SFINA should match test(...) version instead and not issue any error. If I replace U with U(*), then the code compiles again. I couldn't make the sense out of it. What's the magic with (*)? Note that function<int>::yes returns correct result 0 even with U. Please help me out.
0
9826
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10860
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10604
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9389
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6984
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5659
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4026
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.