"Original" typedef do you really understand?

Source: Internet
Author: User

typedef, with the simplest words to interpret him, then is to give the type alias!!! But he's not as easy as you think!
Example:
typedef int SIZE;//then an int has a name of size, after which it can be size I or something;

See here, if you just think that typedef is so much, not just the first parameter (type name), equivalent to the second argument, so simple. Then you may get into trouble.

For example, look at the following example:
typedef char LINE[81];
Can you say line[81] is the alias of Char? This is obviously wrong. What it really means is this: the line type represents an array of characters with 81 elements.
Then line t; is equal to Char t[81]; see here, are you dizzy? Not a good idea, huh? We tried to put it together and see.

Char line[Bayi];           // typedef rows             Char t[Bayi];         // Prototype Line Line      t;          // Apply Row

Similarly:

int size;            // typedef rows        int      i;          // Prototype Line         size    i;            // Apply Row

Another example of a function pointer:

int (*FUN_PTR) (int,int);   // typedef rows int (*FP) (int,int);        // Prototype Line fun_ptr    fp;              // Apply Row

Each of the 3 examples has several things in common:

1, "typedef line" and "prototype line", compared to the form is basically the same, "typedef row" only a number of typedef just. As a last example,

The fundamental difference is that fun_ptr is the alias of the class, and the FP is the variable of that class (in the case of the object, it is the instance of the class).

2. "Prototype line" and "Application line" they get the same result. As a last example, it is all created with a type of

Int (*) (int,int); The function pointer tp.

Summarize:
It is not difficult to see through the above two things in common, or to penetrate a typedef.
Because as long as we can write the prototype line, we can write the TypeDef line directly, and the application of the line of understanding is equivalent to the understanding of the prototype line!
such as int (*FP) (int,int); I want to take an alias with this function pointer type. Just add a typedef in front of you!

typedef int (*FUN_PTR) (int,int), just replace the FP with the alias you want fun_ptr.

The application is much simpler: fun_ptr FP;

The understanding is also simple fun_ptr FP; is equivalent to int (*FP) (int,int);

In fact, the key to understanding is the typedef behind the two parameters as a whole, everything is enlightened ~ ~

"Original" typedef do you really understand?

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.