1. The typedef is intended to make alias of a variable type. The define is mainly to name a constant.
2 typedef unsigned char BYTE;
4 #define MAX_LENGTH(100)
5 #define SUB(x,y) ((x)*(y))
2. The typedef is function during the compile process, which does the type verification. The define is function during the preprocess, no type verification only substitution.
3. The typedef has the effective scope while define does not.
4. When applied to pointer, typedef and define have different result.
02 typedef char* pChar;
04 int main()
06 pCHAR a,b;
07 pChar x,y;
08 char c=0,d=1;
10 const pCHAR p1=&c;
11 const pChar p2=&d;
16 cout<<“size of a is “<<sizeof a<<endl;
17 cout<<“size of b is “<<sizeof b<<endl;
18 cout<<“size of x is “<<sizeof x<<endl;
19 cout<<“size of y is “<<sizeof y<<endl;
20 return 0;
size of a is 4
size of b is 1
size of x is 4
size of y is 4
The p1 is a constant pointer to char*, which can be modified but the content can not. It is like const char* p1.
The p2 is a pointer to constant char*, which can not be modified but the content can. It is like char* const p2.