1. Define new type name for basic data types
typedef double REAL;
// do NOT need to change source code
#ifndef _SIZE_T_DEFINED
#ifdef _WIN64
typedef unsigned __int64 size_t;
#else
typedef _W64 unsigned int size_t;
#endif
#define _SIZE_T_DEFINED
#endif
2. Define/simplify struct/union/enum names
struct tagPoint
{
int x;
int y;
};
// In C codes, "struct" has to be added before.
struct tagPoint pnt;
// In C++, you can use tagPoint to define variables directly
tagPoint pnt;
typedef struct tagPoint
{
int x;
int y;
} Point;
// Then you can use "Point" directly in C codes.
Point pnt;
3. Define a simple name for array
typedef int INT_ARRAY_10[10];
INT_ARRAY_10 arr;
4. Define a simple name for pointer
int *(*a[10])(int, char*);
// usually used to define callback function
// func
typedef int *(*func)(int, char*);
// use new type func
func a[10];
5. Tips - In general cases, typedef is preferred than #define
// case 1
typedef char* pStr1;
#define pStr2 char*;
// s1 and s2 are both char* type
pStr1 s1, s2;
// s3 is char* type and s4 is char type
pStr2 s3, s4;
// case 2
typedef char* pStr;
char str[4] = "abc";
const char *p1 = str;
const pStr p2 = str;
p1++; // p1 is pointer, so p1++ works
p2++; // wrong !!! const type cannot do ++, it is as same as const type p2;