函数指针数组作为函数的参数 [英] Function pointer array as parameter to a function

查看:177
本文介绍了函数指针数组作为函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个特定的问题,即将函数指针数组作为

参数传递给函数。我试图使用一个函数,它将一个

函数指针数组作为参数。我非常确定调用它的语法




#include< stdio.h>


void fp1()

{printf(" in fp1 \ n"); }


void fp2()

{printf(" in fp2 \\\
");}


void fp3()

{printf(" in fp3 \\\
");}


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(.....);

}


有人可以让我知道调用

接受函数指针数组的函数的正确语法。


先谢谢。

Ar

解决方案

ru ********* @ gmail.com 写道:


大家好,


我有一个特定的问题,将函数指针数组作为

参数传递给一个功能。我试图使用一个函数,它将一个

函数指针数组作为参数。我非常确定调用它的语法




#include< stdio.h>


void fp1()

{printf(" in fp1 \ n"); }


void fp2()

{printf(" in fp2 \\\
");}


void fp3()

{printf(" in fp3 \\\
");}


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(.....);

}


有人可以让我知道调用

接受函数指针数组的函数的正确语法是什么。



不是


call_fpn(pfn);


够了?


>

提前致谢。

Ar



-

Pietro Cerutti


5月19日下午4:18,aruna.mys ... @ gmail .com写道:


大家好,


我有一个特定的问题,将函数指针数组作为

函数的参数。我试图使用一个函数,它将一个

函数指针数组作为参数。我非常确定调用它的语法




< snip c code>


有人可以让我知道调用

接受函数指针数组的函数的正确语法是什么。



#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);


返回0;

}


ar ********** @ gmail.com 写道:


我有一个特定的问题,将函数指针数组传递为一个函数的
参数。我试图使用一个函数,它将一个

函数指针数组作为参数。我非常确定调用它的语法




你的尝试看起来已经相当不错了。


#include< stdio.h>


void fp1()



我认为最好准确指定论证的类型

(或者没有预期参数):


void fp1(void)


{printf(" in fp1 \ n"); }


void fp2()

{printf(" in fp2 \\\
");}


void fp3()

{printf(" in fp3 \\\
");}


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

{

(* fp [0] )();

(* fp [1])();

(* fp [2])();

}



您可以将其简化为


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

{

fp [0]();

fp [1]();

fp [2]() ;

}


括号后跟一个函数指针调用函数,

不需要取消引用指针。 />

如果你不想限制自己使用

固定大小的数组,只需使用


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

void main()



main()总是返回一个int,所以make it


int main(void)


{

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



您可以使用定义进行初始化:


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


pfn [0] =& fp1;



单独的函数名称已经是指向

函数的指针(至少当它被用作值时) ,所以''&''

是没有必要的。


pfn [1] =& fp2;

pfn [2] =& fp3;


call_fpn(.....);



只需传递''pfn''作为参数:


call_fpn(pfn);


返回0;


}



问候,Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de

\ __________________________ http://toerring.de


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

解决方案

ru*********@gmail.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


On May 19, 4:18 pm, aruna.mys...@gmail.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;
}


ar**********@gmail.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


这篇关于函数指针数组作为函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆