C-language typedef

Source: Internet
Author: User

Although #define语句看起来象typedef, there are actually differences in nature. For #define来说, the source code is only treated for string substitution prior to compilation, and for typedef It establishes a new data type alias. This shows that pch1 is defined as a pointer variable, but does not implement the programmer's intention, but instead defines PCH2 as a char variable.

In pointer functions, there is a class of functions that also return pointers, but this pointer is not a basic type, such as int, char, but rather a pointer to a function. For beginners, do not write such a function declaration, is to see this writing is also confused. For example , the following statement:

int (*ff (int)) (int *, int);

We use the method described above to analyze theFF first with the following "()" combination, namely:

Int (*(FF (int)))) (int *, int); enclose the FF (int) in parentheses.

It also means thatFF is a function.

Then, in conjunction with the previous "*", it is stated that the return value of the FF function is a pointer. Then it is combined with the following "()", which means that the pointer is pointing to a function.

This kind of writing is really very difficult to understand, so that some beginners have misunderstood, think that people can not understand the code to show their level of high. In fact, in contrast, the ability to write easy-to-understand code is a measure of whether a programmer is good or not. In general, using the TypeDef keyword makes the declaration easier to understand. In the front we have seen:

Int (*PF) (int *, int);

In other words,pf is a function pointer " variable ". When a typedef declaration is used, the pf becomes a function pointer " type ", i.e.:

typedef int (*PF) (int *, int);

This defines the type of the return value. Then, use PF as the return value to declare the function :

PF ff (int);

C-language typedef

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.