typedef bool (* remove_fn)(node const * v)是什么意思?

shaode01 2013-05-25 12:31:39
typedef struct node{
struct node * next;
....
} node;
typedef bool (* remove_fn)(node const * v);
// Remove all nodes from the supplied list for which the
// supplied remove function returns true.
// Returns the new head of the list.
node * remove_if(node * head, remove_fn rm){
for (node * prev = NULL, * curr = head; curr != NULL; )
{
node * const next = curr->next;
if (rm(curr))
{
if (prev)
prev->next = next;
else
head = next;
free(curr);
}
else
prev = curr;
curr = next;
}
return head;
}
上面是一段简化的代码,转自http://wordaligned.org/articles/two-star-programming
我想把他完整成可以运行的,其中这句
typedef bool (* remove_fn)(node const * v);
我查了下typedef的用法,这里的作用是为复杂的声明定义一个新的简单的别名。那这里是为了哪个声明定义的别名呢?写程序的时候是不是要先写一个这样的声明
bool (* func)(node const *v);
func就是一个函数指针,那是不是还得声明一个函数然后赋值
bool f(node const *v);
func=f;
这样以后就可以这样使用了:
remove_fn rm;就是定义一个名为rm的函数指针
rm=f;就是为rm赋值为函数f的首地址


...全文
705 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dust_dust 2013-05-25
  • 打赏
  • 举报
回复
bool (*)(node const*)本身不是一个声明,bool (func*)(node const*)才是一个声明。但为了给某一类型起别名,比如给char *起别名PCHAR,以后用PCHAR a 代表char *a,这里为了给指向“参数为node const*返回值为bool的函数”的指针起别名,就写成了 typedef bool (* remove_fn)(node const * v); 就这么规定的,楼主不要纠结了
shaode01 2013-05-25
  • 打赏
  • 举报
回复
引用 1 楼 hugett 的回复:
对。。这句就是定义了remove_fn是bool (*)(node const*)的别名。。使用上就是你说的那样。。
为复杂的声明定义一个新的简单的别名,这里bool (*)(node const*)是一个声明吗? typedef一般的用法是typedef 原名 别名,为什么这里就那么奇怪?
hugett 2013-05-25
  • 打赏
  • 举报
回复
对。。这句就是定义了remove_fn是bool (*)(node const*)的别名。。使用上就是你说的那样。。
shaode01 2013-05-25
  • 打赏
  • 举报
回复
经过验证,确实是可加可不加,谢谢各位
// testprimer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdlib.h>
typedef struct node
{
	int a;
	struct node * next;
}node;
typedef bool (* remove_fn)(node  *v);

bool func(node  *v)
{
	(*v).a=(*v).a+1;
	return(NULL==v);
}
node * remove_if(node * head, remove_fn rm)
{
	for (node * prev = NULL, * curr = head; curr != NULL; )
	{
		 node * const next = curr->next;
		 if(rm(curr))
		 {
			 if(prev)
				 prev->next=next;
			 else
				 head=next;
			 free(curr);
		 }
		 else
			 prev=next;
		 curr=next;
	}
	return head;
}

int _tmain(int argc, _TCHAR* argv[])
{
	remove_fn rm=func;
	node *x=NULL;
	bool (*f)(node  *v);
		f=func;
	node y;
	y.a=0;
	x=&y;
//	*x=y;
	if((*rm)(x)) printf("x==NULL y.a=%d\n",y.a );

	else printf("x!=NULL y.a=%d\n",y.a);
	if(f(x)) printf("x==NULL y.a=%d\n",y.a);
	else printf("x!=NULL y.a=%d\n",y.a);
	getchar();




	return 0;
}

bewinged 2013-05-25
  • 打赏
  • 举报
回复
定义之后,都指向同一个函数,调用时是没区别的,typedef的作用就是别名。 你是说这个(* func)(a)和rm(a)? 其实都一样,可加*可不加 还是指下面这个? 这个是规定的,没什么好说,用别名就可以包含* 再比如 #define tde int * #typedef uu int * 那么 tde a,b; ----->int *a,b; 与 uu a,b; ----->int *a,*b; 区别就大了
shaode01 2013-05-25
  • 打赏
  • 举报
回复
引用 4 楼 shaode0101 的回复:
另外楼主叙述得好混乱,整理一下是这样子,要想将函数f的首地址赋给一个函数指针有两个办法, 一个是直接声明一个函数指针bool (* func)(node const *v);然后func=f,以后调用函数f就可以通过 (* func)(a)。这是函数指针的用法,之所以多此一举用指针是因为你可以给func赋值为其他函数的首地址,更灵活。 一个是给所有这种类型的函数指针起一个别名,就是typedef bool (* remove_fn)(node const * v);然后remove_fn rm=f;调用函数f就可以通过rm(a)。这是typedef的用法,作用是如果有同样类型的函数要同时用到,就不必为每一个函数声明一个函数指针,只需remove_fn rm2=f2,remove_fn rm3=f3,。。。typedef提供了通用性。
引用 5 楼 zengzhihao 的回复:
typedef bool (* remove_fn)(node const * v); 与bool (* remove_fn)(node const * v); 是完全不同的 第一个没有分配空间,只是给bool(*)(node const)的类型一个别名为remove_fn, 而bool (* remove_fn)(node const * v),是有分配空间的,且指向这个空间的地址指针为remove_fn 再者 就向你说的那样要使用第一个的话 还要用这个别名来定义变量才行 有这样一个函数bool f(node const *v); remove_fn rm;就是定义一个名为rm的函数指针 rm=f;rm就指向了f 如果是第二个的话 就直接是 remove_fn=f了;
那调用函数的时候有区别吗?是不是像4楼说的那样?为什么函数指针就得加*号?定义个别名就不用加*了?
bewinged 2013-05-25
  • 打赏
  • 举报
回复
typedef bool (* remove_fn)(node const * v); 与bool (* remove_fn)(node const * v); 是完全不同的 第一个没有分配空间,只是给bool(*)(node const)的类型一个别名为remove_fn, 而bool (* remove_fn)(node const * v),是有分配空间的,且指向这个空间的地址指针为remove_fn 再者 就向你说的那样要使用第一个的话 还要用这个别名来定义变量才行 有这样一个函数bool f(node const *v); remove_fn rm;就是定义一个名为rm的函数指针 rm=f;rm就指向了f 如果是第二个的话 就直接是 remove_fn=f了;
dust_dust 2013-05-25
  • 打赏
  • 举报
回复
另外楼主叙述得好混乱,整理一下是这样子,要想将函数f的首地址赋给一个函数指针有两个办法, 一个是直接声明一个函数指针bool (* func)(node const *v);然后func=f,以后调用函数f就可以通过 (* func)(a)。这是函数指针的用法,之所以多此一举用指针是因为你可以给func赋值为其他函数的首地址,更灵活。 一个是给所有这种类型的函数指针起一个别名,就是typedef bool (* remove_fn)(node const * v);然后remove_fn rm=f;调用函数f就可以通过rm(a)。这是typedef的用法,作用是如果有同样类型的函数要同时用到,就不必为每一个函数声明一个函数指针,只需remove_fn rm2=f2,remove_fn rm3=f3,。。。typedef提供了通用性。

69,369

社区成员

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

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