C expert programming-Analysis of the signal function prototype declaration {void (* signal (INT Sig, void (* func) (INT)} (2)

Source: Internet
Author: User

In the ANSI standard, the signal () statement is as follows:

Void (* signal (INT Sig, void (* func) (INT)

Signal is a function that returns a function pointer. the function that the latter points to (the return value of signal) accepts an int parameter and returns void. The signal function has two parameters: SIG (INT type) and func (void (*) (INT ).

Void (* func) (INT) is a function pointer. The function to which it points accepts an int parameter, and the return value is void.

The following is simplified using typedef:

Typedef void (* ptr_to_func) (INT );

Ptr_to_func signal (INT, ptr_to_func );

An example on the Internet:

# Include <stdio. h>

Enum{Red, green, blue };

Void outputsignal (intSIG)
{
Printf ("the signal you/'ve input is :"
);

Switch(SIG)
{
Case
Red:
Puts ("red! "
);
Break
;

CaseGreen:
Puts ("green! "
);
Break
;

CaseBlue:
Puts ("blue! "
);
Break
;
}
}

Void (* signal (INT Sig, void (* func) (INT) (int)
{
Puts ("Hello, world! "
);

Func (SIG );

ReturnFunc;
}

Int main (void)
{
(* Signal (Green ,&
Outputsignal) (red );

Return 0;
}

Output:

Hello, world!

The signal you 've input is: Green!

The signal you 've input is: red!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.