The definition and usage of the pointer and function type of typedef function __ function

Source: Internet
Author: User

1, first give an interview question:

#include "stdafx.h"

#include "stdio.h"

Realize the value of the self increase 1

INT Inc (int a)

{

return (++A);

}

To achieve a multiplication of numeric values

Intmulti (INT*A,INT*B,INT*C)

{

return (*C=*A**B);

}


Defines a function type FUNC1, which has an integer parameter that returns an int type data. At this point FUNC2 is a function type.

Typedefint (FUNC1) (int in);

Defines a function type FUNC2, which has three integer pointer parameters, and returns an int type data. At this point FUNC2 is a function type.

typedef int (FUNC2) (int*,int*,int*);

void Show (Func2*fun,int arg1, INT*ARG2)

{

FUNC1 *p=inc; Defines a function pointer to int Inc (int a)

int temp =p (arg1);//temp=arg1+1;

Fun (&TEMP,&ARG1,ARG2); The Fun function type contains three parameters

printf ("%d\n", *arg2);

}

int main (int argc,char* argv[])

{

int A;

Show (Multi,10,&a); Value is 10*11=110

return 0;

}

2, the above code in the

Defines a function type FUNC1, which has an integer parameter that returns an int type data. At this point FUNC2 is a function type.

Typedefint (FUNC1) (int in);

Defines a function type FUNC2, which has three integer pointer parameters, and returns an int type data. At this point FUNC2 is a function type.

typedef int (FUNC2) (int*,int*,int*);

The reference method is:

FUNC1 *p=&inc;

FUNC2 *fun=&multi;

You can also define the form as a function pointer:

Defines a function pointer of type FUNC1, which points to a formal parameter with an int type and returns a function of type int, at which point FUNC1 is a function pointer

Typedefint (*FUNC1) (int in);

Defines a function pointer of type FUNC2, which points to a formal parameter with only three int* types, and returns a function of type int, at which point FUNC2 is a function pointer

typedef int (*FUNC2) (int*,int*,int*);</span>

The reference method is:

FUNC1 p=&inc;

FUNC2 fun=&multi;

3, in the use of the process, the middle bracket remember to use.

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.