Reference:
typedef usage Http://www.cnblogs.com/ggjucheng/archive/2011/12/27/2303238.html
#define用法: http://blog.csdn.net/benny5609/article/details/2314541
====typedef=====
There are generally two uses for typedef: one is to give the variable a new name that is easy to remember and makes sense, and the other is to simplify some of the more complex type declarations.
eg
typedef int size;
typedef unsigned int WORD;
typedef with arrays, pointers
General Array Declarations:
Char text1[81];
Char Text2 [81];
Can do this:
typedef char CHARREF[81];
Charref text1,text2;
General pointer declarations:
Char *ptext1;
Char *ptext2;
Can do this:
typedef char * PCHAR;
PChar PText1;
PChar PText2;
typedef and Functions
A general function pointer declaration:
void Printfhello (int i);
void (*PFUNC) (int);
PFunc = &printfHello;
(*pfunc) (110);
You can see that declaring a function pointer is more complex, and when you declare a pointer variable of the same type in multiple places, the code is more complex, so it can be simplified with typedef:
typedef void (*TYPEFUNC) (int);
Typefunc PFunc;
PFunc = &printfHello;
(*pfunc) (110);
Other local declarations require only this:
Typefunc Pfuncother;
This will be more concise and understandable.
===== #define =====
Today, we have compiled some # define usage to share with you!
1. Simple define Definition
#define MAXTIME 1000
The definition of define is more like a simple text substitution than one that is used as a quantity, and this problem is particularly evident below.
"Function definition" of 2.define
Define can accept some parameters like a function, as follows
#define MAX (x, y) > (y)? (x):(y);
This "function" does not have a type check, like a function template, of course, it is absolutely not as safe as a template. Can be used as a simple template.
3. Single-line definition of macros
#define A (x) t_# #x
#define B (x) #@x
#define C (x) #x
We assume that: X=1, there are:
A (1)------〉t_1
B (1)------' 1 '
C (1)------"1"
(Refer to Hustli's article here)
Multi-line definition for 3.define
Define can replace multiple lines of code, such as the macro definition in MFC (very classic, although it makes people look disgusting)
#define MACRO (arg1, arg2) do {/
/* declarations */
STMT1; /
STMT2; /
/* ... */ /
} while (0)/* (no trailing;) */
The key is to add a "/" to each line break
Excerpt from http://www.blog.edu.cn/user1/16293/archives/2005/115370.shtml patched a few bugs
4. In the large-scale development process, especially in cross-platform and system software, the most important function of define is conditional compilation.
It is:
#ifdef WINDOWS
......
......
#endif
#ifdef LINUX
......
......
#endif
Compile environment can be set by # define at compile time
5. How to define macros, cancel macros
Defining macros
#define [MacroName] [Macrovalue]
Cancel macro
#undef [MacroName]
Normal macro
#define PI (3.1415926)
6. Macros with parameters
(1) #define MAX (a) > (b)? (a), (b))
The key is to be very prone to errors, including differences in machine and human understanding, and so on.
(2) #define ADD (A, b) a+b;
In general use is not a problem, but if encountered such as: C*add (A, b) *d when the problem arises, the algebraic meaning is a+b and then go and c,d multiplied, but because the use of define (it is a simple substitution), so the formula actually becomes: c*a+b*d
Another example:
#define PIN (int*);
Pin A, B;
The intention is that both A and B are int pointers, but actually become int* a,b;a is an int pointer, and b is an int variable. This is the use of TypeDef instead of define, so that both A and B are int pointers.
So when we define it, we develop a good habit and suggest that all levels be bracketed.
6. Conditional compilation
Ifdef ...
(Else) ...
endif
eg
Ifdef dv22_aux_input
Define Aux_mode 3
Else
Define Auy_mode 3
endif
Ifndef XXX ... (#else) ... #endif
7. Prevent duplicate inclusion (repeat definition)
Because the header file contains can be nested, then the C file may contain multiple times the same header file, there may be a duplicate definition of the problem.
Avoid duplicate inclusions with conditional compilation switches (duplicate definitions)
For example
#ifndef __headerfilexxx__
#define __headerfilexxx__
/* File Contents */
#endif
typedef and define basic use