Differences between C/C ++ typedef and # define

Source: Internet
Author: User
In C/C ++, we usually write Program The typedef keyword and # define macro definition commands may often be used. In some cases, using them will achieve the same effect, but they have substantial differences, A keyword is C/C ++, a macro definition command is C/C ++, and typedef is used to create an alias for an existing data type, # define is used to define a macro definition constant. Next, let's talk about the actual usage of the two.

1. typedef keyword

Typedef is used to declare the type alias.CodeTypedef is often used in the process to increase the readability of the Code. It can be an alias for a long string of type names, so using this alias can achieve the same effect as the original type name.

For example:

Typedef int;
Typedef char;

If an alias is set for int and char respectively, the use of int A; and int A; in the program is equivalent. When using typedef, pay attention to the following points:

1) typedef is a new alias for a data type, such as typedef int; so I want to tell you that int represents an integer, typedef int * intptr; this tells us that intptr is a pointer type pointing to an integer variable. This is definitely different from # define. # define is just a simple string replacement and does not express any meaning. For example:

# Define intptr1 int *
Typedef int * intptr2;

Intptr1 P1, P2;
Intptr2 P3, P4;

The effects of intptr1 P1, P2; and intptr2 P3, P4; are definitely different. Intptr1 P1, P2; replace the string with int * P1, P2; declare a pointer variable P1 and an integer variable P2; and intptr2 P3, P4; intptr2 indicates a pointer to the integer data, so both P3 and P4 are pointer variables. This sentence is equivalent to int * P1, * P2. From here, we can see that, macro replacement only replaces strings without any meaning, while using typedef as an alias for a data type has a certain meaning.

Let's look at the example below:

# Define intptr1 int *
Typedef int * intptr2;

Int A = 1;
Int B = 2;
Int c = 3;
Const intptr1 p1 = &;
Const intptr2 P2 = & B;
Intptr2 const P3 = & C;

In the above Code, const intptr1 P1 indicates that P1 is a constant pointer, that is, the content pointed to by P1 cannot be modified through P1, but P1 can point to other content. For const intptr2 P2, intptr2 indicates a pointer type. Therefore, the const is used to restrict the pointer type. Therefore, P2 is a pointer constant and cannot direct p2 to other content, however, you can use p2 to modify the content to which it is currently directed. intptr2 const P3 also declares a pointer constant.

2) macro definition:

# Define int
Unsigned int;

This method is feasible;

While

Typedef int;
Unsigned int;

Is an absolute error.

2. # define macro definition

# Define is a macro definition command used to define a constant (including non-argument constants and parameter constants). It is not executed during compilation, but completed in the preprocessing phase, therefore, no correctness check is performed, and only strings with no meaning are replaced. When macro definition is used, an error will occur if you do not pay attention to it, and this error is often unexpected. For example:

# 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. However, this program does not achieve this effect, because macro replacement only performs simple string replacement, then add (I, j) * k is equivalent to I + J * k, not as expected (I + J) * K.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.