如何在C中指向函数? [英] How to do pointer to function in C?
本文介绍了如何在C中指向函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做指向返回指针的功能
喜欢这个功能
I want to do point to function that return me a pointer
Like this function
int *test(int x)
{
int y=x*x;
int *p=&x;
return p;
}
我的尝试:
我尝试了这个选项int main但我总是得到错误
What I have tried:
I tried this option int main but i get error all the time
int (**t)(int x);
t=&test;
int (**t)(int x);
t=*test;
int (**t)(int x);
t=&(*test);
int (**t)(int x);
t=*(&test);
推荐答案
试试这个
int *(* myfunc)(int x);
myfunc = test;
打电话给
int * ret = myfunc(3);
这对我来说已经有一段时间但是试一试。
注意:您将返回参数变量的地址。一旦你回到调用程序,它就消失了。大错误!!!
Try this
int* (*myfunc)(int x);
myfunc = test;
Call as
int* ret = myfunc(3);
It's been a while for me but give this a try.
Note: You're returning the address of an parameter variable. It's gone once you've returned to the calling routine. Big Error !!!
建议:花时间学习语言,这是一个学习C和C ++的神话讲座,包括答案。
这里是语言作者的C和C ++参考书的链接。注意,C是C ++的祖先,所以知道C对C ++总是有用。
C编程语言 - 维基百科,免费的百科全书 [ ^ ]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2。 pdf [ ^ ]
http://www.ime.usp。 br / ~pf / Kernighan-Ritchie / C-Programming-Ebook.pdf [ ^ ]
C ++编程语言 [ ^ ]
Advice: Take time to learn properly the language, here is a very god lecture to learn C and C++, answer included.
Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]
C++ Programing Language[^]
这篇关于如何在C中指向函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文