指向函数问题的指针 [英] pointers to functions question
问题描述
#include< stdio.h>
#include< stdlib.h>
void func(int);
main(){
void(* fp)(int);
fp = func; //在这里分配指针时,为什么不用fp =& func; ?
(* fp)(1);
fp(2);
退出(EXIT_SUCCESS) ;
}
void
func(int arg){
printf("%) d \ n",arg);
}
#include <stdio.h>
#include <stdlib.h>
void func(int);
main(){
void (*fp)(int);
fp = func; // here when assigning a pointer, why not fp=&func; ?
(*fp)(1);
fp(2);
exit(EXIT_SUCCESS);
}
void
func(int arg){
printf("%d\n", arg);
}
推荐答案
1月2日下午6:22,Logan Lee < logan.w .... @ student.uts.edu.auwrote:
On Jan 2, 6:22 pm, Logan Lee <logan.w....@student.uts.edu.auwrote:
#include< stdio.h>
#include< stdlib.h>
void func(int);
main(){
void(* fp)(int);
fp = func; //在这里分配指针时,为什么不用fp =& func; ?
(* fp)(1);
fp(2);
退出(EXIT_SUCCESS) ;
}
void
func(int arg){
printf ("%d \ nn",arg);
}
#include <stdio.h>
#include <stdlib.h>
void func(int);
main(){
void (*fp)(int);
fp = func; // here when assigning a pointer, why not fp=&func; ?
(*fp)(1);
fp(2);
exit(EXIT_SUCCESS);
}
void
func(int arg){
printf("%d\n", arg);
}
我相信2个选项确切同样的事情......但是会把它留给其他人来引用标准的相关段落。
fp = func;
fp =& func;
问候,
Ivan Novick
http://www.0x4849.net
1月3日上午10点22分, Logan Lee< logan.w .... @ student.uts.edu.auwrote:
On Jan 3, 10:22 am, Logan Lee <logan.w....@student.uts.edu.auwrote:
#include< stdio.h>
#include< stdlib.h>
void func(int);
main(){
void(* fp)(int);
fp = func; //在这里分配指针时,为什么不用fp =& func; ?
(* fp)(1);
fp(2);
退出(EXIT_SUCCESS) ;
}
void
func(int arg){
printf ("%d \ nn",arg);
}
#include <stdio.h>
#include <stdlib.h>
void func(int);
main(){
void (*fp)(int);
fp = func; // here when assigning a pointer, why not fp=&func; ?
(*fp)(1);
fp(2);
exit(EXIT_SUCCESS);
}
void
func(int arg){
printf("%d\n", arg);
}
它们实际上是相同的。选择一个你喜欢的。
They are actually the same. Pick one you like.
Logan Lee< lo ********* @ student.uts.edu.auwrites:
Logan Lee <lo*********@student.uts.edu.auwrites:
#include< stdio.h>
#include< stdlib.h>
void func(int) ;
main(){
#include <stdio.h>
#include <stdlib.h>
void func(int);
main(){
更好:
int main(void){
Better:
int main(void) {
void(* fp)(int);
fp = func; //在这里分配指针时,为什么不用fp =& func; ?
(* fp)(1);
fp(2);
退出(EXIT_SUCCESS) ;
}
void
func(int arg){
printf("%) d \ n",arg);
}
void (*fp)(int);
fp = func; // here when assigning a pointer, why not fp=&func; ?
(*fp)(1);
fp(2);
exit(EXIT_SUCCESS);
}
void
func(int arg){
printf("%d\n", arg);
}
请参阅comp.lang.c FAQ中的问题4.12和1.34,
< http://c-faq.com/> ;.
函数调用实际上需要一个函数指针值才能
括号。在典型的直接中,函数调用,这个指针值
是从函数名中获得的。
函数名(更一般地,函数类型的任何表达式)是
在大多数情况下隐式转换为指向函数的指针。
(例外情况是它是sizeof运算符的操作数,
是非法的,而不是产生指针大小,当它是一元&的
操作数时,它产生函数的地址。)
所以,在
fp = func;
表达式``func'''',它是函数类型,衰变为a
指向函数的指针;结果分配给fp。
在
(* fp)(1);
fp指针指向功能型。应用一元*推断
指针,产生指针类型的结果,它会立即衰减指向函数的指针 - 这就是函数所需要的
电话。
在
fp(2);
fp已经指向指针功能型;结果直接用于函数调用
。
另请注意
func(3);
或
(* func)(3);
是合法的。 (练习:追踪每种情况下会发生什么。)
-
Keith Thompson(The_Other_Keith)< ks *** @ mib.org> < br $> b $ b [...]
我们必须做点什么。这是事情。因此,我们必须这样做。
- Antony Jay和Jonathan Lynn,是部长
See questions 4.12 and 1.34 in the comp.lang.c FAQ,
<http://c-faq.com/>.
A function call actually requires a function pointer value before the
parentheses. In a typical "direct" function call, this pointer value
is obtained from the name of the function.
A function name (more generally, any expression of function type) is
implicitly converted to a pointer to the function in most contexts.
(The exceptions are when it''s the operand of a sizeof operator, which
is illegal rather than yielding a pointer size, and when it''s the
operand of a unary "&", which yields the address of the function.)
So, in
fp = func;
the expression ``func'''', which is of function type, decays to a
pointer-to-function; the result is assigned to fp.
In
(*fp)(1);
fp is of pointer-to-function type. Applying unary "*" deferences the
pointer, yielding a result of pointer type, which immediately decays
to a pointer-to-function -- which is what''s required for the function
call.
In
fp(2);
fp is already of pointer-to-function type; the result is used
directly for the function call.
Note also that either
func(3);
or
(*func)(3);
is legal. (Exercise: Trace what happens in each case.)
--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
这篇关于指向函数问题的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!