typedef and #define的区别

Source: Internet
Author: User

typedef and #define的区别

Organized in a classic blog, the classic original address http://www.cnblogs.com/csyisong/archive/2009/01/09/1372363.html

Case one:

In general, typedef are better than # define, especially where pointers are available. Take a look at the example:

typedef char *PSTR1;

#define PSTR2 char *;

PSTR1 S1, S2;

PSTR2 S3, S4;
In the variable definition above, S1, S2, S3 are defined as char *, and S4 is defined as char, not the pointer variable we expect, the root cause is that # define is simply a string substitution and a typedef is a new name for a type.

Case TWO:

The compiler will report an error in the following code, do you know which statement is wrong?

typedef char * PSTR;

Char string[4] = "ABC";

const char *P1 = string;

Const PSTR P2 = string;

p1++;

p2++;

============ Explanation 1:

It was p2++ that made a mistake. This question reminds us again: TypeDef and # define are different, it is not a simple text substitution. The const PSTR P2 in the above code is not equal to the const char * p2. The const PSTR P2 and const long x are essentially no different, but are read restrictions on variables, except that the data type of the variable p2 is defined by ourselves rather than by the intrinsic type of the system. Therefore, the meaning of const PSTR P2 is that the variable P2 with the qualification data type char * is read-only and therefore p2++ error.


I can't read the above, okay?
You can actually read it several times.
Const PSTR P2 and const long x are essentially no different, and are read-only restrictions on variables. X is a constant. It cannot be changed after the initial session.
The meaning of const PSTR P2 is that the variable P2 with the qualification data type char * is read-only.
It can be understood that P2 is a constant. The P2 itself is not to be changed. But its type is pstr is a char *, which indicates that the data it points to is char, not a const char. Indicates that the data pointed to by P2 is available for modification.

============ Explanation 2:
const char *P1 = string; You can understand this: (const char) *P1 = string, p1 is a pointer to a const char thing, this thing is a string (string is the first address of a character array, its address is definitely const after declaration, unless the array is destroyed), But P1 is a pointer variable, it can be incremented, that is, you see the p1++, it can be done from the array to iterate the purpose of the array.

============ explanation 3:
Onst char *p1 = string means a pointer to the const char type:
The P1 pointer is a variable. He can point to any Char object. Even if it is not a const-defined char, p1 can point to a different char object because he is a variable. But he either points to a const char object or a Char object. Cannot change the value that it points to the object. This means that the example P1 cannot modify the string. Only the value of string can be read.
and
typedef char *PSTR
Const PSTR P2 = string here is a typedef and pointers shared when the pit daddy points out.
Const PSTR P2 = string
PStr const P2 = string
char* const P2 = string above 3 statements is a meaning.
That is, p2 is a const pointer to an object of the non-const, char type. P2 cannot point to a second object after initialization. However, P2 can modify the value of string:

See here you may think this is not to say p2++ right ~ then please continue to see explanation ~

p2++ is wrong. P2 was originally a pointer. And it's a const. This means a constant pointer. Do you think a const constant after initialization can also be + + ... The above explanation is wrong. I don't know what book to read. You can get a program to see. I'll post the code I just test.

#include <stdio.h>

int main () {
typedef char * PSTR;
Char string[4] = "ABC";
const char *P1 = string;
Const PSTR P2 = string;
p1++;
P2[2] = ' e ';
printf (p1);
printf (p2); return 0;
}
The P2 pointer can modify the string. Can not move P2. He is a const constant!!!!!!!!!!!!!!!



Below is the code: I tested it with VS2010. I don't know what the other compilers are like.
#include <stdio.h>

int Main () {
typedef char * PSTR;
char string[4] = "abc";
const char *p1 = string;
const pStr p2 = string;
p1[1] = ' f ';
p1++;
p2[2] = ' e ';
p2++;
printf (p1);
printf (p2);
return 0;
}
Here's the error:
1>c:\test\typedef.cpp (8): Error C3892: "P1": Cannot assign a value to a constant
1>c:\test\ Typedef.cpp (one): Error C3892: "P2": Cannot assign a value to a constant


****
According to my friend's understanding, const PSTR P2 where the const rhetoric is pStr, and the pStr type is actually a pointer, so this line is defined as a constant pointer. It can be understood that the definition of P2 is a constant pointer to char, a constant pointer, p2++ of course not.
***

============ Explanation: 4:
Test for Case Two:

Typedef.c:51:5: Error: Assigning value to read-only location ' *P1 '
Typedef.c:52:5: Error: Make read-only variable ' p2 ' self-increment

My understanding of char * P: two parts, char is a pointing value, p is a pointer variable, * just make a statement;
When the const char * p1, the const modifier is char, so P1 cannot modify the value pointed to;
When const PSTR P2, the const modifier is P2, so P2 can not make any changes to itself;

A typedef is equivalent to creating a new type and can only be viewed as a whole.


============ Explanation 5:
In fact, here is mainly the scope of the role of const understanding.
typedef char * PSTR;
Char string1[4] = "ABC";
Char string2[4] = "Def";
const char *P1 = string1;
Const PSTR P2 = string1;

P1[1] = ' f '; This sentence is wrong
p1++; This sentence to
P1 = string2; This sentence to
P1[1] = ' x '; This sentence is wrong

P2[1] = ' f '; This sentence to
p2++; This sentence is wrong
P2 = string2; This sentence is wrong

In the const char *P1, the function of the Const force in P1 refers to the content, that is, when P1 points to string1, the content of string1 is constant relative to P1, is fixed, can not be changed, so p1[1] = ' F ' This sentence is wrong, The value of P1 itself (that is, the address value) can be changed, so both p1++ and P1 = string2 are correct.

In the const PSTR P2, the function of the Const force in the P2, that is, P2 is a constant, the value of P2 is actually an address, that is, the P2 is fixed, its value is immutable, but it refers to the content of the address can be changed, so p2[1] = ' f ' is right, but p2 = String2 is wrong, because this sentence is to assign another address to P2.

In general, one is to fix the content to a constant, one is to fix the address to a constant, that is, the pointer constant and constant pointer, p1 is a constant pointer, p2 is a pointer constant. This is also the charm of pointers in C.

Insert language:
Just look at the const non-change typedef const Modification is *P1 that is *p1 this value can not be moved that can not be assigned to the const modifier is P2, that is, P2 can not move, but *P2 to assign value

Error C3892: ' P2 ': cannot assign to a variable it is const
Look at this vs2008 error should be understood, the type as a system-defined type I think it is better to understand, like:
const int a = 5;
a++;
The compiler error is the same as the truth.

******
Everybody progress together Oh ~ ~

Classic Original Address http://www.cnblogs.com/csyisong/archive/2009/01/09/1372363.html

typedef and #define的区别

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.