Trap question record 1: The difference between macro definition and typedef

I am not a smart person, and I don’t have a deep grasp of knowledge. It is inevitable that I will fall into the trap of some topics. The original intention of recording is to warn myself, and it is also a summary of knowledge points. I hope it will be helpful to you. After all, the reason for the trap It is a trap because it successfully traps people. It is a small note, one question per article.

The topics are as follows:

The test.c file includes the following statement. Among the four variables defined in the file, the variable of pointer type is 【multiple choice】()

#define lNT_PTR int*
typedef int* int_ptr;
INT_PTR a, b;
int_ptr c, d;

A:a       B: b       C:c      D:d


Please think first and then read the answer below


Answer: ACD

Parse:

#define is a macro definition, and its function is just replacement, not a serious type. After replacement, it becomes:

int  *a,b;

Since the macro is just a simple replacement, after replacing it into this format. * will be combined with a first, and a variable of *a will be created with type int, so that a pointer of type int* will be created. This is somewhat contrary to our usual understanding, but it is true.

int e, *f;

 Further down, typedef is a real type alias, this alias is an independent type, creating variables with this type is equivalent to using this type to create variables of this type.

int* c,d;

So, the answer is ACD

in conclusion:

The mistake made in this question is the improper understanding of the difference between macro definition and typedef. The replacement of macro as text will cause errors in details, which needs to be paid attention to in the future.


The analysis is more of my own subjective analysis, and it cannot be guaranteed to be correct! There must be some mistakes, readers are urged to contact me to change! Grateful!

QQ:2624253582

Guess you like

Origin blog.csdn.net/m0_53607711/article/details/128470070