我如何使用变量p来声明一个新的指针使用下面的功能 [英] How can I use variable p to declare a new pointer to function using following
问题描述
我 P
这样的:
的typedef INT(* P)();
让说的功能我想声明一个指针为int 富()
我要宣布一个新的指针使用变量p使用typedef语句来函数foo。
喜欢的typedef INT EMP [10];
EMP磷; //所以现在p是int类型的大小为10的数组
我是那种在C的幼稚所以原谅,如果它听起来很愚蠢......
-
P
是:的typedef INT(* P)();
-
foo()从:
INT富(){
} -
P
输入变量˚F
:点F =&放大器; foo的;
-
如何使用指针调用:
(* F)();
举例code:
#包括LT&;&stdio.h中GT;
INT富(){
的printf(\\ n在FOO \\ n);
返回4;
}
的typedef INT(* p)的();诠释主(){
p F =&放大器; foo的;
INT I =(* F)();
的printf(\\ N I =%d个\\ N,I);
返回1;
}
你可以找到它正在 在codePAD。
注意:您可以的只是的分配如 PF = foo的;
,并呼吁像 F()
第二种形式,你可以在这里找到在 codePAD
编辑:@Akash评论:
它编译和运行,如:
〜$ GCC x.c -Wall
〜$ ./a.out
在FOO
I = 4
这里是一个项目帮助解释功能的实用性指针。
I have p
like:
typedef int (*p) ();
let say the function to which I want to declare a pointer is int foo()
I wanna declare a new pointer to function foo using variable p to make use of typedef statement.
like typedef int emp[10];
emp p;// so now p is an array of size 10 of type int
I am kind of naive in C. so pardon if it sounds stupid...
p
is:typedef int (*p) ();
foo() is:
int foo(){ }
p
type variable isf
:p f = &foo;
how to call using pointer:
(*f)();
Example code:
#include<stdio.h>
int foo(){
printf("\n In FOO\n");
return 4;
}
typedef int(*p)();
int main(){
p f = &foo;
int i = (*f)();
printf("\n i = %d\n", i);
return 1;
}
you can find it is working on codepad.
note: you can simply assign like p f = foo;
and call like f()
the second form you can find here on codepad
Edit: As @Akash commented:
it compiles and runs like:
~$ gcc x.c -Wall
~$ ./a.out
In FOO
i = 4
Here is a project to help explain the usefulness of function pointers.
这篇关于我如何使用变量p来声明一个新的指针使用下面的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!