Calios Co-language CONST

xiaoxiao2021-03-06  13

Basic explanation const is a keyword of a C language that 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.

Problem: Const variable & constant

Why do I use a const variable to initialize the array in justice, 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 Variable & 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) Basic forms used by Const: const char m;

Limited M unality.

2), replace the M, Const char * PM in 1 form;

Limited * PM is not variable, of course, PM is variable, so P1 is pair in the problem.

3) Replace 1 type char, const newtype m;

Limited M unality, Charptr in the problem is a new type, so P2 is not variable, and P2 is wrong.

Question: Const variable & string

What is the 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

Can 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 small; it defines an array of size 3, which is initialized to "ABC", note that it does not have a usual string terminator '/ 0', so this The array only 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

The type declaration is used to modify a constant, there are two ways to write, then, if you ask, what is the constant content with constant limited?

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 constant

2), const is behind, with the above statement

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 variable char const * const pContent; // pContent and * pContent are constant

Answer and analysis:

CONST and pointers are used together in C language a very common confusion, in actual development, especially when seeing others code, often because it is not good to judge the author's intentions, let's talk about my judgment principle :

Along the * number one line, constell is 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.

转载请注明原文地址:https://www.9cbs.com/read-49040.html

New Post(0)