函数指针数组 [英] array of pointers on functions

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

问题描述

你好


我很长时间都没有触及任何C代码,我不记得如何使用
声明函数指针数组。我试过这个:

-----------------------

35 void firing_1

36(解雇f,

37 int * old_m,

38 int * new_m);

39

40 const void(* firing_table [1])

41(解雇,

42 int *,

43 int *) =

44 {

45 kick_1

46};

---------- --------------


但编译器说:

__firings __。h:46:警告:从不兼容的初始化指针

类型


我的gcc版本是2.8.1。


任何想法?


感谢您的回复

解决方案

Evangelista Sami写道:

你好

很长一段时间我都没有触及任何C代码,我不记得如何在函数上声明一个指针数组。我试过这个:
-----------------------
35 void firing_1
36(射击f,
37 int * old_m,
38 int * new_m);
39
40 const void(* firing_table [1])
41(射击,
42 int *,
43 int *)=
44 {
45 firing_1
46};
---------------- --------

但编译器说:
__firings __。h:46:警告:从不兼容的指针初始化
类型




Typedef通常在这种情况下有所帮助。


typedef void FIRING_FUNCTION(触发f,int * old_m,int * new_m);


FIRING_FUNCTION firing_1;


int main(无效)

{

const FIRING_FUNCTION * firing_table [1 ];


firing_table [0] = firing_1;


返回0;

}

-

Richard Heathfield: bi****@eton.powernet.co。英国

Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton


Evangelista Sami写道:


你好

我很久没碰到任何C代码了不记得如何在函数上声明一个指针数组。我试过这个:
-----------------------
35 void firing_1
36(射击f,
37 int * old_m,
38 int * new_m);
39
40 const void(* firing_table [1])
41(射击,
42 int *,
43 int *)=
44 {
45 firing_1
46};
---------------- --------

但是编译器说:
__firings __。h:46:警告:从不兼容的指针初始化
类型

我的gcc版本是2.8.1。

任何想法?/ * BEGIN new.c * /




#include< stdio.h> ;

#include< math.h>


#define FUNCTION_LIST {sin,cos,tan}

#define STRING_LIST {" sin"," cos"," tan"}

#define PI_OVER_4(3.14159265 / 4)

#define FUNCTIONS(sizeof function / sizeof * function )


int main(无效)

{

double(* function [])(double)= FUNCTION_LIST;
char * string [] = STRING_LIST;

size_t result;


for(result = 0;结果!=功能; ++结果){

printf("%s(%f)是%f \ n",

string [result],PI_OVER_4,function [result] (PI_OVER_4));

}

返回0;

}


/ * END new .c * /


-

pete


Evangelista Sami写道:


你好

我很久没碰到任何C代码而且我不记得如何在函数上声明一个指针数组。我试过这个:
-----------------------
35 void firing_1
36(射击f,
37 int * old_m,
38 int * new_m);
39
40 const void(* firing_table [1])
41(射击,
42 int *,
43 int *)=
44 {
45 firing_1
46};
---------------- --------

但是编译器说:
__firings __。h:46:警告:从不兼容的指针初始化
类型

我的gcc版本是2.8.1。

任何想法?

感谢您的回复




Jirka Klaue亲切地告诉我这个:


double(* f [])(double)= {sin,cos,log,exp}; //指针数组

for(i = 0; i< 4; i ++)printf("%f\ n",f [i](。5)); //前使用


函数可以是任何东西,只要它们在参数的类型中是一致的,并且在类型他们返回。


可能是一个好主意,你想要指出任何函数原型

到_before_你指向他们在表中; - )


-

Julian V. Noble

物理荣誉教授
jv *@lessspamformother.virginia.edu

^^^^^^^^^^^^^^^^^^
< a rel =nofollowhref =http://galileo.phys.virginia.edu/~jvn/target =_ blank> http://galileo.phys.virginia.edu/~jvn/


科学只知道一条诫命:为科学做出贡献。

- Bertolt Brecht,Galileo。


hello

i haven''t touch any C code for long time and i dont remember how to
declare an array of pointers on functions. i have tried this :
-----------------------
35 void firing_1
36 (firing f,
37 int *old_m,
38 int *new_m);
39
40 const void (*firing_table[1])
41 (firing,
42 int *,
43 int *) =
44 {
45 firing_1
46 };
------------------------

but the compiler says :
__firings__.h:46: warning: initialization from incompatible pointer
type

my gcc version is 2.8.1.

any idea?

thanks for any response

解决方案

Evangelista Sami wrote:

hello

i haven''t touch any C code for long time and i dont remember how to
declare an array of pointers on functions. i have tried this :
-----------------------
35 void firing_1
36 (firing f,
37 int *old_m,
38 int *new_m);
39
40 const void (*firing_table[1])
41 (firing,
42 int *,
43 int *) =
44 {
45 firing_1
46 };
------------------------

but the compiler says :
__firings__.h:46: warning: initialization from incompatible pointer
type



Typedefs usually help in this kind of situation.

typedef void FIRING_FUNCTION(firing f, int *old_m, int *new_m);

FIRING_FUNCTION firing_1;

int main(void)
{
const FIRING_FUNCTION *firing_table[1];

firing_table[0] = firing_1;

return 0;
}
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


Evangelista Sami wrote:


hello

i haven''t touch any C code for long time and i dont remember how to
declare an array of pointers on functions. i have tried this :
-----------------------
35 void firing_1
36 (firing f,
37 int *old_m,
38 int *new_m);
39
40 const void (*firing_table[1])
41 (firing,
42 int *,
43 int *) =
44 {
45 firing_1
46 };
------------------------

but the compiler says :
__firings__.h:46: warning: initialization from incompatible pointer
type

my gcc version is 2.8.1.

any idea?/* BEGIN new.c */



#include <stdio.h>
#include <math.h>

#define FUNCTION_LIST { sin , cos, tan }
#define STRING_LIST {"sin", "cos", "tan"}
#define PI_OVER_4 (3.14159265 / 4)
#define FUNCTIONS (sizeof function / sizeof *function)

int main(void)
{
double (*function[])(double) = FUNCTION_LIST;
char *string[] = STRING_LIST;
size_t result;

for (result = 0; result != FUNCTIONS; ++result) {
printf("%s(%f) is %f\n",
string[result], PI_OVER_4, function[result](PI_OVER_4));
}
return 0;
}

/* END new.c */

--
pete


Evangelista Sami wrote:


hello

i haven''t touch any C code for long time and i dont remember how to
declare an array of pointers on functions. i have tried this :
-----------------------
35 void firing_1
36 (firing f,
37 int *old_m,
38 int *new_m);
39
40 const void (*firing_table[1])
41 (firing,
42 int *,
43 int *) =
44 {
45 firing_1
46 };
------------------------

but the compiler says :
__firings__.h:46: warning: initialization from incompatible pointer
type

my gcc version is 2.8.1.

any idea?

thanks for any response



Jirka Klaue kindly showed me this:

double (*f[])(double) = {sin, cos, log, exp}; // array of pointers
for (i=0; i<4; i++) printf("%f\n", f[i](.5)); // ex. of use

The functions can be anything, as long as they are consistent
in the type(s) of their argument(s), and in the type they return.

Probably a good idea to prototype any functions you want to point
to _before_ you point to them in the table ;-)

--
Julian V. Noble
Professor Emeritus of Physics
jv*@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".


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

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