c语言的函数声明和typedef - fun - fun

c语言的函数声明和typedef

abelard posted @ 2011年5月26日 08:55 in C语言 , 2259 阅读

在《UNIX环境高级编程》、《UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking API》都讨论信号处理,并且都解释了下面这个函数原型:

void (*signal (int signo, void (*func) (int))) (int);

没搞懂,只好找到《c 陷阱与缺陷》对函数定义的解释,初步的理解是:

1. 函数的返回类型可以是基本类型和复合类型,还可以是函数.上面的原型,就属于返回类型为函数:

void (*func)(int);

2. 如果使用typedef定义上面的函数,看起来是要舒服多了,但是理解起来是一样的:

typedef void (*func)(int);

func signal(int, func pFunc);

(注:感觉理解的还很肤浅)

做两个例子来看看到底是怎么用的:

#include <stdio.h>

typedef void (*signal_handler)(int);

void (*(signal_hand(int, void sig(int))))(int);

signal_handler pSig_Handle(int , signal_handler);

signal_handler pSig_Handle(int a, signal_handler p)
{
  printf("1111 %d \n",a);
  return(p);
}
void func(int a){printf("2222 %d\n",a);}

void (*(signal_hand(int a, void sig(int x))))(int b){
  printf("3333 %d \n",a);
  return(sig);
}


main()
{
  signal_handler psigHandler = pSig_Handle(50,func);
  signal_handler psigHand = signal_hand(200,func);
  
  psigHandler(100);
  psigHand(300);

  signal_hand(200,func)(5000);
}

输出的结果是:

$ ./a.out
1111 50 
3333 200 
2222 100
2222 300
3333 200 
2222 5000

Avatar_small
Canara Bank Online B 说:
2022年8月06日 22:12

Canara Bank has been providing service to their customers since 1906 and in this meantime it has seen great growth, and the banking network in modern technology and providing easy services to every customer in their footsteps has emerged with great technology growth. Canara Bank Online Banking Canara bank net banking service by giving various options to the customer which enables them not to walk to the branch office to use every option.Bank as well got their branch setup virtually with having multiple customer support assistance which ensure that their customer account creates online and as well get used to other services.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee