Example of a function whose return value is a function pointer

Source: Internet
Author: User

Link: http://www.cnblogs.com/super119/archive/2011/03/26/1996145.html

 

A question from interview, so I wrote a test program to demonstrate.

Func is a function pointer. the return value of this function is int without any input parameters. Then, the main program declares a function pointer myfuncpointer, which indicates that the input parameter of the function pointer represents an int, the return value is also a function pointer, And the type is func type. So we can assign the func2 function to the pointer of myfuncpointer. The question of interview is the declaration of myfuncpointer.

# Include <stdio. h>

Typedef int (* func )();

Int func3 ()
{
Printf ("in function 3.../N ");
Return 0;
}

Func func2 (int)
{
Printf ("input parameter is: % d/N", );
Return func3;
}

Int main ()
{
Func (* myfuncpointer) (INT) = func2;
Func returnvalue = myfuncpointer (1111 );
Int func3_return = returnvalue ();
Printf ("func3 return value is: % d/N", func3_return );
Return 0;
} Original article link: Workshop
# Include <stdio. h>

Int get_big (int I, Int J)
{
Return I> = J? I: J;
}

INT (* F (int A) (INT, INT) // F (int A) is a function. The return value of this function is
{// Pointer to the function (the return value of this function is int and has two int-type parameters)
Printf ("A = % d/N", );
Return get_big;
}

Int main (void)
{
Int Max;
INT (* p) (INT, INT); // defines a pointer pointing to a function (the return value of this function is int, and two of them are int parameters ).
P = f (100 );
Max = P (5, 8 );
Printf ("max = % d/N", max );
Return 1;
}

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.