[C language] notes the difference between #define and typedef

1、#define

define a preprocessing command without any checking at compile time, for only the simple substitution

The general form of the macro is defined as:

#define 宏名 字符串

Here's the string is a sequence of characters in the general sense, and not to the C language equivalent of the string, it does not require double quotes.

2、typedef

typedef is used as a simple declaration defines aliases complex in the C language, which itself is the key A storage class, with auto, extern, mutable, static, register and other keywords not appear in the same expression.

typedef alias general form is:

typedef  旧名字  新名字

3, with the difference define the typedef

Without after the semicolon (1) #define, typedef tape after the semicolon.

(2) #define specifier may use other types of expanded macro type names, and so do not typedef. Such as:

#define INT1 int
unsigned INT1 n;  //没问题
typedef int INT2;
unsigned INT2 n;  //有问题

INT1 unsigned type specifier can be extended, but can not use the unsigned expand INT2.

(3) when continuous define several variables, typedef to ensure that all variables are defined the same type, and #define is not guaranteed. Such as:

#define PINT1 int*;
P_INT1 p1,p2;  //即int *p1,p2;
typedet int* PINT2;
P_INT2 p1,p2;  //p1、p2 类型相同

PINT1 different type defined p1 and p2, p1 i.e. pointer variable to point to shaping, shaping p2 is variable; PINT2 defined p1 and p2 of the same type, i.e. all point type int pointer.

Let's look at an important issue regarding the typedef! ! Look at the code:

#include <stdio.h>

typedef char *pStr;

int main(void)
{
    char string[4]="abc";   //第一行代码
    const char *p1=string;  //第二行代码
    const pStr p2 = string; //第三行代码
    p1++;                   //第四行代码
    p2++;                   //第五行代码 

    return 0;
}

This code compiler will report an error: error: increment of read-only variable 'p2'You know what the problem is?

The answer and analysis:

Is the fifth line of code p2 ++ wrong. This question reminds us: different typedef and #define, it is not a simple text replacement. The above code const pStr p2 is not equal const char * p2. No const pStr on p2 and pStr const p2 essential difference, all the variables are read-only restrictions, but here the data type of the variable p2 is our own definition inherent in the system rather than type it.

Thus, const pStr p2 is the meaning: defined as a char data type of the variable p2 is read-only, that is, char const p2, p2 is a pointer to indicate the type often char pointer, p2 can not be modified, so p2 ++ error.

Some examples of the way and the meaning of the const statement:

const int a;     //①
int const b;     //②
const int *c;    //③ 
int * const d;   //④  
int const * e const;  //⑤ 

①a is a constant, unchangeable;

②b is a constant, unchangeable;

③c integer constant is a pointer and the pointer can be changed, can not change the contents of pointer;

④d constant pointer is a pointer to an integer variable, the pointer can not be changed, the content pointer can be changed;

⑤e pointer is a pointer to an integer constant is constant, and a pointer to a pointer variable content not.


My personal blog: https://zhengnianli.github.io/

My micro-channel public number: Embedded hodgepodge

Guess you like

Origin www.cnblogs.com/zhengnian/p/11493969.html