Reading 6 ("c Expert programming")

Source: Internet
Author: User

The use of a typedef that is uncommon but worth mentioning:

1. use typedef to define platform-independent types.


For example, define a floating-point type called REAL. On the target platform one. The type that allows it to represent the highest precision is:
typedef long double REAL;
on platform two that does not support long double, replace the following:
typedef double REAL;
on three platforms that are not supported by double, replace the following:
typedef float REAL;
That is, when cross-platform, only need to change the typedef itself, no other source codedo whatever changes are made.


This technique is widely used in the standard library. For example size_t.
Also, because TypeDef is a new alias that defines a type. is not a simple string substitution.


2. Define a new, simple alias for the complex declaration.

The method is: in the original declaration, gradually replaced with the alias part of a complex declaration, so loop. Leave the part with the variable name to the last substitution. The most streamlined version of the original statement was obtained.

Example:

(1). Original declaration: Int * (*A[5]) (int, char*);
The variable is named a, and replacing a with a new alias Pfun is possible:
typedef int * (*PFUN) (int, char*);
The most streamlined version of the original statement:
Pfun A[5];

(2). Original declaration: Void (*b[10]) (void (*) ());
The variable is named B. Replace the right part of the parentheses with Pfunparam as alias one:
typedef void (*pfunparam) ();
Replace the left variable B,pfunx with the alias two:
typedef void (*PFUNX) (Pfunparam);
The most streamlined version of the original statement:
Pfunx B[10];

(3). Original statement: Doube (*) () (*e) [9];
The variable is named E, and the left part is replaced first. Pfuny for alias One:
typedef double (*pfuny) ();
Then replace the variable e to the right. Pfunparamy as alias two
typedef PFUNY (*pfunparamy) [9];
The most streamlined version of the original statement:
Pfunparamy e;

Examples of understanding complex declarations:
Int (*func) (int *p);
Func is a function pointer that has a int* type of shape, and the return value type is int.
Int (*func[5]) (int *);
The right side of the Func is a [] operator, which indicates that Func is an array with 5 elements, and the left side of Func has a *. The element that describes Func is a pointer (note here that the * is not decorated with func, but rather modifies func[5], because the [] operator has precedence higher than *. Func is combined with [] first. Jump out of this parenthesis. Look to the right, and then the parentheses, stating that the element of the Func array is a function pointer, and that the function it points to has a int* type of formal participation. The return value type is int.

Remember 2 modes:
Type (*) (...) --Function pointer
Type (*) []--Array pointer


Other:

1. typedef char * PSTR;

Char string[4] = "ABC";

const char *P1 = string;

Const PSTR P2 = string;

p1++;

p2++;

p2++ compilation error.

typedef and # define are different, it is not a simple text substitution. The const PSTR P2 in the above code is not equal to the const char * p2. The const PSTR P2 and the const long x are essentially no different, and are simply read-only restrictions on variables, except that the data type of the variable p2 is our custom rather than the intrinsic type of the system. Therefore, the meaning of the const PSTR P2 is that a variable with a qualified data type of char * p2 is read-only and therefore p2++ error.

In accordance with the order. ' Const PSTR ' is interpreted as ' char * const ' (a constant pointer to char) instead of ' const char * ' (a pointer to a constant char). This problem is very easy to solve:

typedef const CHAR * CPSTR; int mystrcmp (CPSTR, CPSTR); It's right now.

Remember: no matter what time. To declare a typedef for pointers only, add a const to the last typedef name to make the pointer itself a constant, not an object.


2. typedef and Storage class keyword

A typedef is like a auto,extern,mutable. Static, like register, is a storage class keyword. This is not to say that typedef really affects the storage characteristics of an object, but only on the statement composition, the typedef declares a variable declaration that looks like a type such as Static,extern. The following will take you to a second trap:

typedef register INT Fast_counter; Error

Compile pass just. The problem is that you cannot have multiple storage class keyword in the declaration. Because the symbol typedef has occupied the location of the storage class keyword. Register (or whatever other storage class keyword) cannot be used in a typedef declaration.


References Link: http://www.ourlove520.com/Programming/vc/201404/242796.html

6 Reading ("C expert programming")

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.