C and C++ details typedef and define

  • 2020-05-12 03:01:17
  • OfStack

typedef and #define in C/C++

Preface:

In C/C + +, we usually write programs may often used typedef keywords and # define macro definition command, in some cases, can achieve the same effect using them, but they are a substantial difference between the two, one is C/C + + keywords, one is the macro definition of C/C + + command, typedef used to an existing data type 1 individual name, and # define is used to define a macro definition constants. Let's talk about what we should pay attention to when using them in practice.

1. typedef keyword

While typedef is used to declare type aliases, typedef is often used in the actual coding process to increase the readability of the code. It can have a single name for a very long type name, so using this alias will have the same effect as the original type name.

Such as:


typedef int INT;
typedef char CHAR;

If you have a single name for int and char, use INT a in your application. And int a; The effect achieved is equivalent. The following points should be noted when using typedef:

1)typedef is a new alias for a data type, such as typedef int INT; So what you're going to tell me is INT for integer, typedef int* INTPTR; It tells us that INTPTR is a pointer to an integer variable, unlike #define, which is a simple string substitution that expresses nothing. Such as:


#define INTPTR1 int*
typedef int* INTPTR2;

INTPTR1 p1,p2;
INTPTR2 p3,p4;

INTPTR1 p1 p2; And INTPTR2 p3 p4; The effect of these two sentences is decidedly different. INTPTR1 p1 p2; int* p1,p2, int* p1,p2; The meaning to be expressed is to declare 1 pointer variable p1 and 1 integer variable p2; While INTPTR2 p3 p4; Since INTPTR2 is a pointer to integer data, p3 and p4 are pointer variables. This is equivalent to int* p1,*p2; As you can see here, macro substitution does not involve any meaningful substitution, just string substitution; An alias using typedef for a data type has a definite meaning of 1.

Here's another example:


#define INTPTR1 int*
typedef int* INTPTR2;

int a=1;
int b=2;
int c=3;
const INTPTR1 p1=&a;
const INTPTR2 P2=&b;
INTPTR2 const p3=&c;

In the above code, const INTPTR1 p1 indicates that p1 is a constant pointer, that is, it is not possible to modify the content pointed to by p1 through p1, but p1 can point to other content; For const INTPTR2 p2, since INTPTR2 represents a pointer type, const is used to define it, which means that the pointer type is blocked. Therefore, p2 is a pointer constant, and p2 cannot be made to point to other contents, but it can be modified through p2. INTPTR2 const p3 also declares a pointer constant.

2) for macro definition:


#define INT int
unsigned INT a;

This usage is possible;

while


typedef int INT;
unsigned INT a;

Absolutely wrong usage.

2. # define macro definition

#define is a macro definition command that defines a constant (both parameter free and parameter free) that is not executed during compilation, but is done in the preprocessing phase, so no correctness checks are made and only string substitutions without meaning are performed. When using macro definitions, errors can occur if you are not careful, and they are often unexpected. Such as:


#define ADD(a,b) a+b

int i=1;
int j=2;
int k=3;
int s=ADD(i,j)*k;

The program may want to calculate the result of (i+j)*k, but this program does not achieve this effect. Since the macro substitution is simply a string substitution, ADD(i,j)*k is equivalent to i+j*k, not i+j)*k.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: