CONST use analysis [original] Chuyun 2003-08-07
Webmaster Reviews: C programming language as a language-oriented programming of superiority, and the language of structural programming language, it is destined to be no longer possible in a considerable period of time. The language replaced. This is no doubt. "Const use analysis" is a classic problem in our team, and now it is made up of C spikes Chu Yun, and analyzes it to help you understand the use of cconst and its key grammar. ZENGYI820 ------------------------------------------------- ---------------- CONST defined modifier is the focus of C learning, it is mainly used in functions to protect the object it modified, and improve the reliability of the code It is very helpful, so "using constive as possible" (see Effective C ) has become a criterion for C programming. However, some basic usage of Const is where beginners are often easier to confuse, and this article describes some of the more difficult places in Const. -------------------------------------------------- ----------------- First let's pay attention to the two ways of const:
Const
INT A = 1024;
/ / I am equivalent to int const a = 1024;
These two ways are equivalent, and most people will choose the first way. However, I think the second way is better, I will give an example: people who have seen C Primer should remember that there is such a statement when talking to Typedef:
Typedef
CHAR * CSTRING;
Const
CString CSTR;
// What do you understand?
I think many people see that this statement will think that this is const char * cstr; but unfortunately, const is modtring, and CString represents char * is not char, that is, converts the pointer to constant, called char * Const CSTR; (About the difference between the two I will explain the following section). If we use const, you will not misunderstand the cstring const cstr; is it easy to understand? So I recommend using the second way (actually I am not very accustomed to J). -------------------------------------------------- ------------------- Let's see a row of statements:
Const
INT P = 1024;
/ / How to define a pointer to point to constant p
Does the definition below?
INT * PI = & P;
// is wrong, you can't use a very quantity pointer to constant P
Const
INT * PI2 = & P;
// There is no doubt that this is right.
Let's look at a definition:
// Is it very strange? Add a const, what is different?
Const
Int *
Const pi3 = & p;
Different, before answering this question, let's explain the subtle place of Const Int * Pi2: We can read this statement, PI2 is a pointer (PI2 itself is not constant), it points to a constant P (it pointing object P It is constant). This will be understood! We can give PI2 to other values, causing it to different objects, but cannot modify the object points to the PI. The following example can be a good description.
Const
INT P = 1024;
Const
INT * PI2 = & P; * pi2 = 0;
// is wrong, we can't modify the value of P
INT A = 2;
PI2 = & A
/ / Can give PI2 other values
Note: The value of PI2 points may not be a Const type, but we still cannot modify the object pointing to PI2. Now let's take another statement int * const pi4; we just want to think about the above statement, PI4 is a constant pointer, we can't modify it, but you can change it to the object, Hoho, very simple. ! Proud the above two statements, get a const INT * const pi3 = & p; answer comes J ... -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------- We look at a statement
Const
INT & IR = 1024;
Strange? This statement is correct! The reason is that 1024 is an impersonable value. For such values to implement reference, the compiler must generate a temporary object with reference to this temporary object.
INT TEMP = 1024;
/ / Give this temporary object a name TEMP
Const
INT & IR = TEMP;
/ / The reference point is it
Then why must I add a const in INT & IR? Suppose INT & IR is correct, then if you give a new value to IR, it will change the value of TEMP. If it is very bad! So add consT, the compiler will consider it correct. -------------------------------------------------- ------------------- has the preparedness of the above two sections, let us do such a topic:
Const
INT P = 1024;
Require the address of the Const object P to initialize a reference? This question has two solutions, first say the first one, divided into two steps:
Const
INT * PI = & P;
// Pi itself is a very amount of pointers, pointing to a const object P
Const
INT * & PP = Pi;
/ / The reference point is a very amount of pointer Pi
Here, the pointer PI pointing by the PP is a very amount of a pointer, which can reassign it, but cannot modify the value of P. Look at the second:
Const
Int *
CONST & PI2 = & P;
I believe you can understand this statement ^ - ^! ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------- Well, write is almost the same, write it down, I am afraid of joke ^ 0 ^, thank you After reading this small article, I hope to have a little help to you, huh, let us make progress together. The main reference books in this article are C Primer