Difference Between Typedef and #define

xiaoxiao2021-03-05  20

First, You CAN Extend A Macro TypeName with Other Type Specifiers, But not a typef 'D TypeName.

That IS,

#define peach int

Unsigned peach i; / * Works fine * /

Typedef int banana;

Unsigned banana i; / * bzzzt! Illegal * /

Second, a typedef 'D name provides the Type for Every Declarator in a Declaration.

#define int_ptr Int * int_ptr chalk, cheese;

After macro expansion, the second line effectively becomes: int * chalk, cheese; This makes chalk and cheese as different as chutney and chives: chalk is a pointer-to-an-integer, while cheese is an integer.

In contrast, a typedef like this:

Typedef char * char_ptr;

CHAR_PTR BENTLEY, ROLLS_ROYCE;

Declares Both Bentley and Rolls_royce to Be The Same. The name on The Front Is Difer, But They Are Both a Pointer To a char.

转载请注明原文地址:https://www.9cbs.com/read-38588.html

New Post(0)