A deep analysis of the problem of C-language typedef and complex function declaration

Source: Internet
Author: User
The following is a detailed analysis of the TypeDef and complex function declarations in C language, the need for friends can come to reference the next

Here are three variable declarations, I would like to use typedef to define an alias for them, how do I do?
>1:int * (*a[5]) (int, char*);
>2:void (*b[10]) (void (*) ());
>3. Doube (*) () (*PA) [9];
Answer and Analysis:
A simple way to create a type alias for a complex variable is simply to replace the variable name with the type name in the traditional variable declaration expression, and then add the keyword typedef to the beginning of the statement.

>1:int * (*a[5]) (int, char*);
Pfun is a type alias we built.
typedef int * (*PFUN) (int, char*);
Declares an object using a new type of definition, equivalent to int* (*a[5]) (int, char*);
Pfun A[5];
A is an array of 5 elements, the element of which is a function pointer, and the return value of the function pointer refers to a pointer to type int, with two input parameters, the first int, and the second char*

>2:void (*b[10]) (Void (*) ());
First declare a new type for the red part of the expression above
typedef void (*pfunparam) ();
Declare a new type as a whole
typedef void (*pfun) (Pfunparam);
Declares an object with the new type defined, equivalent to void (*b[10]) (void (*) ());
Pfun B[10];
b is an array of 10 elements, each element is a function pointer, the function pointer refers to the return value of the function is void, the input parameter is another function pointer, this function pointer has no input parameters, the return value is void

>3. Doube (*) () (*PA) [9];
//First declare a new type for the red part of the expression above
typedef double (*pfun) ();
Declare a new type as a whole
typedef pfun (*pfunparam) [9];
Declares an object with the new type defined, equivalent to Doube (*) () (*PA) [9];
Pfunparam PA;

PA is a pointer to an array of 9 elements, the element of the array is a function pointer, the function pointer does not have an input parameter, the return value is double

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.