Water drop stone wearing C language correctly using const
Chu Yunfeng 2004-9-16 14:46:00 / Basic explanation const is a keyword of a C language, which defines a variable that is not allowed to be changed. Using const can improve the robustness of the program to a certain extent, in addition, when watching others code, clear understanding of the role of Const, there are some helps to understand the other party. Although this sounds very simple, in fact, the use of Const is also a subtle place in the C language, where is it? Please see the following questions. Question: Const Variable & Constant Why is the example of the following example to initialize the array, ANSI C's compiler reports an error?
Const int N = 5; Int a [n]; answer and analysis: 1), this problem discusses the difference between "constant" and "read-only variable". Constants are definitely read-only, such as 5, "ABC", etc., must be read-only because there is no place to store its value at all, and of course it is not able to modify it. The "read-only variable" is a place to store it in memory, but this value is defined by the compiler that is not allowed to be modified. C Language Keyword Const is used to qualify a variable that is not allowed to be changed. Qualifier. The variable N is modified as a read-only variable in the above code, but it is not a constant. The dimension must be "constant" when the array definitions specify the array definitions, and "read-only variable" is not possible. 2) Defining a constant, this way is pair), in fact, according to the compilation process and memory allocation, this usage should be reasonable, just the provisions of the ANSI C pairs to array. 3), then what is the constant in the ANSI C language? The answer is an enum type and #define macro, both of which can be used to define constants. Question: Const Variables & Const Limited Content The following code compiler will report an error. Which statement is wrong?
Typedef char * pstr; char string [4] = "abc"; const char * p1 = string; const pstr p2 = string; p1 ; p2 ; answer and analysis: The problem is in P2 . 1), the basic form of Const use: const char m; defining m is not variable. 2), replace the m, const char * pm in the 1 style; define * PM is not variable, of course, the PM is variable, so P1 is pair in the problem. 3), replace 1 type char, const newtype m; define M unality, the charptr in the problem is a new type, so P2 is not variable, P2 is wrong. Question: Const variable & string is constantly questionable? Is there any problem with the code below?
Char * p = "I'm Hungry!"; P [0] = 'I'; answer and analysis: The above code may cause illegal write operations of memory. The analysis is as follows, "I'm Hungry" is essentially a string, and constant is often placed in a read-only memory area and cannot be written. p Initially point to this read-only memory, and P [0] = 'i' attempts to write this place, the compiler will certainly not agree. Question: Const variable & string constance 2 CHAR A [3] = "ABC" legal? What hidden dangers do it use? Answer and analysis: This is legal in standard C, but its living environment is very narrow; it defines an array of size 3, which is initialized to "ABC", note that it does not usually string terminator '/ 0 ', So this array just looks like a string in the C language, which is essentially, so all functions for processing the string, such as strcpy, printf, etc., can not be used on this false string. Question 5: Const & Pointer Type Declaration CONST is used to modify a constant, there are two ways to write, then, what is the content that is unambiguous with const below? 1), const in front const Int nvalue; // Nvalue is constconst char * pContent; // * PContent is const, PContent variable const (char *) PContent; // pContent is const, * PContent variable char * const pContent; // pContent is const, * pContent variable const Char * const pContent; // pContent and * PContent are const 2), const is behind, with the above declaration
INT const nvalue; // nvalue is constchar const * pContent; // * PContent is const, pContent variable (char *) const pContent; // pContent is const, * PContent variable char * const pContent; // pContent is constant , * PContent; // PContent and * PContent are all Const Answers and Analysis: CONST and pointers are used in a C language a very common confusion, in actual development, especially When someone else's code, often because it is not good to judge the author's intention, let's talk about my judgment principle: along the * number one line, const and who are on the side, then who is const, the constant element is it. You can look at the actual significance of the above declaration according to this rule, and I believe that the order will be at a glance. In addition, it is necessary to note: For const (char *); because char * is a whole, it is equivalent to a type (such as char), so this is constative pointer to const.