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

问题描述

我正在尝试使用函数指针。不幸的是,我的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;
}

推荐答案




-




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




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



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





你已经得到了(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:

C库:


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

"你用湿面条判处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++


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

08-20 11:01
查看更多