请问这样的类型定义怎么用?

andyboy 2003-03-25 11:20:01
typedef int (*tt)(void *, void *);
这样定义有什么作用?
...全文
36 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Flamesong 2003-03-25
  • 打赏
  • 举报
回复
gz
zhouzhaohan 2003-03-25
  • 打赏
  • 举报
回复
Sorry tt func[]={aa,bb,cc};中去掉cc, tt func[]={aa,bb,cc};
不是定义函数指针变量,而是函数指针类型,定义函数指针变量是
int (*tt)(void *, void *);
zxm954712 2003-03-25
  • 打赏
  • 举报
回复
tt is a pointer to a function that takes two parameters(void *, void *) and return a integer.

Thus, you can assign it to a real function address and execute it.
for example:
#include <iostream>
using namespace std;

void func() {
cout << "func() called..." << endl;
}

int main() {
void (*fp)(); // Define a function pointer
fp = func; // Initialize it
(*fp)(); // Dereferencing calls the function
void (*fp2)() = func; // Define and initialize
(*fp2)();
} ///:~

zhouzhaohan 2003-03-25
  • 打赏
  • 举报
回复
定义一个函数指针类型,返回值为int,参数为两个void类型的指针.

typedef int (*tt)(void *, void *);
int aa(void *a,void *b)
{
......
}
int bb(void *a,void *b)
{
......
}
tt func[]={aa,bb,cc};
int cc;
void *a,*b;
.....
cc=(*func[0])(a,b);
Jinglihui 2003-03-25
  • 打赏
  • 举报
回复
函数名调用和函数指针调用是等效的!
但是后者比前者更灵活,在很多用函数名调用不可以的情况下,用指针调用就很好
你可以在google里搜索"函数指针"就会找出很多这方面应用的好例子
wkpeng 2003-03-25
  • 打赏
  • 举报
回复
楼上说的没错
cenlmmx 2003-03-25
  • 打赏
  • 举报
回复
定义一个函数指针,返回值为int,参数为两个void类型的指针.

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧