函数指针作为函数参数 [英] function pointers as function parameters

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

问题描述

我正在尝试使用函数指针。不幸的是,我的C书在功能指针上没有任何功能参数。我想将一个

指针传递给ff()到f(),结果f()打印ff()的返回值

。下面的代码似乎有用,但我很感激你的

评论。我做对了吗?函数名称是否为decay?指针?

#include< stdio.h>

/ *声明一个带参数的函数

这是一个指针一个函数返回一个int * /

void f(int(* fptr)());

/ *函数返回一个int * /

int ff(void);


int main(无效)

{

f(ff); / *将ff的地址传递给f * /


返回0;

}


void f( int(* fptr)())

{

int a;

a =(* fptr)(); / * deref func指针* /

printf("%d \ n",a);

return;

}


int ff(无效)

{

返回2345;

}

I am experimenting with function pointers. Unfortunately, my C book has
nothing on function pointers as function parameters. I want to pass a
pointer to ff() to f() with the result that f() prints the return value
of ff(). The code below seems to work, but I would appreciate your
comments. Have I got it right? Does the function name "decay" to a pointer?
#include <stdio.h>
/* declares a function which takes an argument
that is a pointer to a function returning an int */
void f(int (*fptr)());
/* function returning an int */
int ff(void);

int main(void)
{
f(ff); /* pass the address of ff to f */

return 0;
}

void f(int (*fptr)())
{
int a;
a = (*fptr)(); /* deref the func pointer */
printf("%d\n", a);
return;
}

int ff(void)
{
return 2345;
}

推荐答案




Marlene Stebbins写道:


Marlene Stebbins wrote:
我正在尝试使用函数指针。不幸的是,我的C书在功能指针上没有任何功能参数。我想将指向ff()的指针传递给f(),结果f()打印ff()的返回值
。下面的代码似乎有效,但我将非常感谢您的评论。我做对了吗?函数名称是否为decay?一个指针?


可能更容易说函数名称是

a指向函数的指针,因为
$中没有上下文b $ b它除了腐烂之外​​还做任何其他事情。


你的代码是正确的(据我所见),但是那里

风格改进的几个机会:

#include< stdio.h>
/ *声明一个带参数的函数
这是一个返回函数的指针int * /
void f(int(* fptr)());


你知道更多关于指向函数而不是它返回值的

类型:具体来说,你知道

它不需要任何参数。 (你怎么知道这个?因为

你在调用它时没有提供任何参数。)关于一般

原则,最好告诉编译器一切

你知道,把声明写成


void f(int(* fptr)(void));


这样,如果你无意中试图用参数调用指向函数,编译器会抗议。如果

你实际上想要传递参数,请这样说:


void f2(int(* fptr)(double,int,const char *) );

/ *函数返回int * /
int ff(void);

int main(void)
{
F(FF); / *将ff的地址传递给f * /


这里是'ff'衰变的地方。

返回0;
}

void f(int(* fptr)())


如果你提供了上面建议的原型,

你也应该在这里这样做。

{
int a;
a =(* fptr)(); / * deref func指针* /


这也可以写成`a = fptr();''没有

括号和星号。有些人喜欢写这个电话,因为你已经这么做了,并说这引起了人们对函数指针变量(而不是

a函数标识符)正在使用中。我个人并不是b $ b买这个论点,注意到


(* printf)(Hello,world!\ n);


同样合法,(恕我直言)同样愚蠢。然而,

de gustibus non disputandum est(拉丁语中的只有

与Gus无争论)。

printf( %d \ n,a);
返回;
}
int ff(void)
{
返回2345;
}
I am experimenting with function pointers. Unfortunately, my C book has
nothing on function pointers as function parameters. I want to pass a
pointer to ff() to f() with the result that f() prints the return value
of ff(). The code below seems to work, but I would appreciate your
comments. Have I got it right? Does the function name "decay" to a pointer?
It might be simpler to say that the function name "is"
a pointer to the function, since there is no context in
which it does anything other than "decay."

Your code is correct (as far as I can see), but there
are a few opportunities for stylistic improvement:
#include <stdio.h>
/* declares a function which takes an argument
that is a pointer to a function returning an int */
void f(int (*fptr)());
You know more about the pointed-to function than the
type of its returned value: specifically, you know that
it takes no arguments. (How do you know this? Because
you supply no arguments when you call it.) On the general
principle that it''s best to tell the compiler everything
you know, write the declaration as

void f(int (*fptr)(void));

This way, the compiler will protest if you inadvertently
try to call the pointed-to function with arguments. If
you do in fact want to pass arguments, say so:

void f2(int (*fptr)(double, int, const char*));
/* function returning an int */
int ff(void);

int main(void)
{
f(ff); /* pass the address of ff to f */
Here''s where ff "decays."
return 0;
}

void f(int (*fptr)())
If you''ve provided a prototype as suggested above,
you should also do so here.
{
int a;
a = (*fptr)(); /* deref the func pointer */
This can also be written `a = fptr();'' without the
parentheses and the asterisk. Some people prefer to write
the call as you''ve done it, saying that it draws attention
to the fact that a function pointer variable (rather than
a function identifier) is being used. I personally don''t
buy that argument, noting that

(*printf)("Hello, world!\n");

is equally legitimate and (IMHO) equally silly. However,
de gustibus non disputandum est (Latin for "There''s just
no arguing with Gus").
printf("%d\n", a);
return;
}

int ff(void)
{
return 2345;
}




-
Er * ********@sun.com



--
Er*********@sun.com


Eric Sosman写道:
Eric Sosman wrote:

Marlene Stebbins写道:

Marlene Stebbins wrote:
我正在尝试使用函数指针。
不幸的是,我的C书中没有关于函数指针的函数参数。
我想通过指向ff()到f()的指针,其结果是f()
打印ff()的返回值
。下面的代码似乎有效,但我将非常感谢您的评论。我做对了吗?
函数名称是decay吗?一个指针?
I am experimenting with function pointers.
Unfortunately, my C book has
nothing on function pointers as function parameters.
I want to pass a
pointer to ff() to f() with the result that f()
prints the return value
of ff(). The code below seems to work, but I would appreciate your
comments. Have I got it right?
Does the function name "decay" to a pointer?



可能更简单的说,函数名称是是指向函数的指针,因为
中没有上下文它除了衰变之外还做任何其他事情。



It might be simpler to say that the function name "is"
a pointer to the function, since there is no context in
which it does anything other than "decay."




函数名称仍然是函数类型的表达式

当它是地址运算符的操作数。

您可以通过调用它来强制非宏调用标准库

函数

(& putchar) )(''\ n'');

如果putchar是指针则没有意义。

如果函数名是指针表达式,

那么它将是sizeof运算符的有效操作数。

出于这两个原因,

我不会说函数名是一个指针。


-

pete



The function name remains an expression of a function type
when it is an operand of the address operator.
You can force a non macro invocation of a standard library
function by calling it like
(&putchar)(''\n'');
which wouldn''t make sense if putchar were a pointer.
If a function name were a pointer expression,
then it would be a valid operand of the sizeof operator.
For those two reasons,
I would not say that a function name is a pointer.

--
pete


Marlene Stebbins在02/05/05写道:
Marlene Stebbins wrote on 02/05/05 :
#include< stdio.h>
/ * d eclares一个带参数的函数
是一个返回int * /
void f的函数的指针(int(* fptr)());
/ *函数返回一个int * /
int ff(void);

int main(无效)
{
f(ff); / *将ff的地址传递给f * /

返回0;
}

void f(int(* fptr)())
{
int a;
a =(* fptr)(); / * deref func指针* /
printf("%d \ n",a);
返回;
}

int ff(void)
{
返回2345;
}
#include <stdio.h>
/* declares a function which takes an argument
that is a pointer to a function returning an int */
void f(int (*fptr)());
/* function returning an int */
int ff(void);

int main(void)
{
f(ff); /* pass the address of ff to f */

return 0;
}

void f(int (*fptr)())
{
int a;
a = (*fptr)(); /* deref the func pointer */
printf("%d\n", a);
return;
}

int ff(void)
{
return 2345;
}




你已经得到了(jist添加''void''进入( )它一切都很好。现在,尝试简单的方式




#include< stdio.h>


typedef int F (无效);


static int ff(无效)

{

返回2345;

}


静态无效f(F * pf)

{

/ * deref func指针* /

int a = pf();


printf("%d \ n",a);

return;

}


int main(无效)

{

/ *将ff的地址传递给f * /

f(ff);


返回0;

}


- -

Emmanuel

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

C库: http://www.dinkumware.com/refxc.html


显然,您的代码不符合原始规格。

"你用湿面条判处30睫毛。

- Jerry Coffin在a.l.c.c ++中



You''ve got it (jist add ''void'' into the () and it''s all fine). Now, try
the Simple Way:

#include <stdio.h>

typedef int F (void);

static int ff (void)
{
return 2345;
}

static void f (F * pf)
{
/* deref the func pointer */
int a = pf ();

printf ("%d\n", a);
return;
}

int main (void)
{
/* pass the address of ff to f */
f (ff);

return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++


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

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