开发者

Confused by #define and typedef

开发者 https://www.devze.com 2023-02-19 02:08 出处:网络
#define T Stack_T typedef struct T *T; Then what does T in struc开发者_StackOverflow中文版t T mean,the one defined by #define or typedef?#define directives are substituted early on in the compilatio
#define T Stack_T
typedef struct T *T;

Then what does T in struc开发者_StackOverflow中文版t T mean,the one defined by #define or typedef?


#define directives are substituted early on in the compilation process (translation phase 4, compilation doesn't actually occur until phase 7, these phases and what happens during them are detailed in the standard, section 5.1.1.2).

That #define will simply change T pre-processing tokens into Stack_T.

The effect of that on the typedef will be to turn it into:

typedef struct Stack_T *Stack_T;

Following that, Stack_T is defined as a type, a pointer to another type of struct Stack_T. The Stack_T and struct Stack_T are two separate things.


The preprocessor does only do text-substitutions, so that code would look like

typedef struct Stack_T *Stack_T;

So every T in your code is first replaced to Stack_T, and after that your compiler kicks in, sees the typedef and uses struct Stack_T*.

It might be good to know that struct Type and Type are only the same in C++, not in C.


Since #define is processed in pre-compilation and struct in compilation, after the pre-compilation you'll have typedef struct T *T; looks like that: typedef struct Stack_T *Stack_T;


The T represents Stack_T so, you can read the typedef as:

typdef struct Stack_T *Stack_T;

so every T in your code is replaced as Stack_T during compiler compilation.

0

精彩评论

暂无评论...
验证码 换一张
取 消