What is the difference between typedef and define declaration?

The road is hindered and long, and the line is coming. Keep your head down and work hard, if you don't call it, it's a blockbuster! Come on, Sao Nian!

Preface

  In the past, when I used and made a statement, I always felt that there was no difference. The two methods of use were the same. However, when I recently read "C Traps and Defects", I found that there is a big difference, so it is necessary to summarize and record. typedef define

Reference

  Some reference URLs are as follows

  The reference book information is as follows

  • "C Primer Plus Sixth Edition" P478

  • "C Pitfalls and Defects" P100

  • "C++ Primer Plus Sixth Edition" P248

Source of problem

  When I was studying "C Trap and Defect", I encountered such a problem and I didn't understand it. Then I read the information to understand the meaning!

#define T1 struct foo *
typedef struct foo *T2;

T1 a, b;
T2 a, b;

struct foo *a, b;      // 宏定义方式,声明展开,T1
struct foo *a, *b;     // 类型定义方式,声明展开,T2

  After I watched it at the time, I was always confused and didn't know much about this kind of pointer. I still don't understand why the T2 is like this after unfolding?

  If you already understand it, that's great, this is a big difference! If you don't understand it, it doesn't matter, I will analyze it myself.

analysis Summary

  After consulting relevant reference materials, I understood a basic fact;

  • Use the macro definition, just the replacement of the name; define
  • Use type definitions, came out, was a genuineness type . typedef

  Then, I saw another example from "C Primer Plus"

#define BYTE unsigned char      // typedef 与 #define 功能重合

typedef char * STRING;          // #define 没有的功能
STRING name, sign;
char *name, *sign;

#define STRING char *           // 定义多个变量时,导致只有第一个有效。
STRING name, sign;
char *name, sign;

  It can be seen from the above pseudo-code that when only used for name definition, it has the same effect. typedef define

  But when used as other complex type definitions, the results of these two methods are very different!

  For example code, used to define a type of use is defined as follows: typedef char * typedef

typedef char * STRING;   // typedef类型声明
STRING name, sign;       // 变量声明
char *name, *sign;       // 声明展开结果

  But if you use the declared result, it is obviously different. The sample code is as follows: define

#define STRING char *    // 声明STRING,当遇到STRING时,替换为char *
STRING name, sign;       // 变量声明
char *name, sign;        // 声明展开结果

  As you can see from the above code, the result of using the statement is that in the preprocessing process, when the statement named STRING is encountered , it is replaced with (char *) intact , nothing more! (You can think of other traps used here , and I will summarize them in detail later.) define define

  Briefly summarize the reasons

  • If there is no keyword, the compiler will recognize STRING as a pointer variable to char ; typedef
  • If a keyword, the compiler put STRING interpreted as a type of identifier , the type is a pointer to char pointer; typedef

to sum up

  1. typedef In this way, the compiler will STRING interpreted as a type of identifier ! This process is the compiler process!
  2. define In this way, just replace the name out of it, this process is in the preprocessor treated!
  3. Remember these two conclusions to avoid more similar pitfalls.
  4. Again, just replace the name, and is the type identifier! ! ! define typedef

If the content of the article is wrong, please comment / private message a lot of advice, thank you! If you think the content of the article is not bad, remember to click three links (like, bookmark, leave a message), your support is my greatest encouragement, thank you!

Guess you like

Origin blog.csdn.net/fighting_boom/article/details/108813445