IOS typedef function pointer usage

Source: Internet
Author: User

Simplified codeTo promote cross-platform development.

Typedef acts a bit like # define macro and replaces the synonym with its actual type.

Difference: typedefInterpreted during compilationSo let the compiler handleText replacement beyond pre-processor capabilities.

Usage 1:

Typedef int(* Myfun)(INT, INT);
This method is generally usedFunction Definition aliasTime
The example above definesMyfunIsFunction pointerThe function type includes two int parameters, and an int is returned.

InAnalysisWhen defining this form, you can use the following method:
Remove typedef and alias first,The rest is the type of the original variable.
Remove typedef and myfun:
 
INT (*) (INT, INT)

Usage 2:

Typedef defines a variable typeAlias.

TypedefStruct {
Int;
Int B;
}My_type;

Here, an unnamed structure is taken directly as an alias named my_type, so that if you want to define the structure of the instance, you can do this:
My_type TMP;

Second usage: typedef Original variable type Alias

Simple function pointer usage

// Form 1: return type (* function name) (parameter table)

Char (* pfun) (INT );

//Typedef Char (*Pfun) (INT) // the same function as the previous line

/* The function of typedef isDefine a New Type. The first sentence isDefines a ptrfun typeAnd define this typePointer to a functionThis type of function takes an int as the parameter and returns the char type. */

Char glfun (int A) {return ;}

Void main ()

{

Pfun = glfun;

(* Pfun) (2 );

}

The first line defines a pointer variable.Pfun. It is a pointer to a function. This function parameter is of the int type and the return value is of the char type.We cannot use this pointer only in the first sentence, because we have not assigned a value to it..

The second row defines a function.Glfun ().This function is a function that returns char with int as the parameter. We need to understand the function at the pointer level-The function name is actually a pointer.,Function NamePointThe first address of the function code in the memory.

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.