How to use const correctly in C language

  • 2020-04-01 23:37:02
  • OfStack

The basic explanation
Const is a C keyword that qualifies a variable that cannot be changed. To some extent, using const can improve the robustness of the program. In addition, it is also helpful to clearly understand the role of const when viewing other people's code.
While this may sound simple, the use of const is actually one of the more subtle aspects of c. Look at the following questions.

Question: const variables & constants
Why does ANSI C's compiler report an error when I initialize the array with a const variable as in the following example?
Const int n = 5;
Int a [n].

Answer and analysis:
1) this question discusses the difference between "constants" and "read-only variables". Constants must be read only, such as 5, "ABC", etc., must be read only, because there is no place in the program to store its value, and of course it cannot be modified. A read-only variable, on the other hand, creates a place in memory to store its value, which is limited by the compiler and cannot be modified. The C keyword const is a Qualifier that qualifies a variable that is not allowed to be changed. In the above code, the variable n is modified as a read-only variable, but no amount of modification is constant. ANSI C states that the dimensions must be "constant" when an array is defined, and that "read-only variables" are not allowed.

2), note: in ANSI C, this is an error, because the size of the array should be a constant, and const int n,n is just a variable (constant! = an immutable variable, but in standard C++ this is defined as a constant, which is correct). In fact, this usage should be reasonable in terms of compilation and memory allocation, except that ANSI C restricts arrays.

3) so, what defines a constant in ANSI C? The answer is the enum type and the #define macro, both of which can be used to define constants.

Problem: const variable & const qualified 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 + +;
The p2 + +;

Answer and analysis:
The problem is p2++.
1) basic const form: const char m;
M is immutable.
2) replace m, const char * PM in formula 1;
The limit * PM is immutable, of course PM is variable, so p1++ is correct in the problem.
3) replace type 1 char, const newType m;
M is immutable. Charptr in the problem is a new type, so p2 is immutable in the problem. P2 ++ is wrong.

Problem: const variable & string constant
What's wrong with the following code?
Char *p = "I'm hungry!" ;
P [0] = 'I';

Answer and analysis:
The above code may cause illegal writes to memory. As an analysis, "I'm hungry" is essentially a string constant, and constants are often placed in read-only memory by the compiler and cannot be written. P initially points to this read-only memory region, and p[0] = 'I' attempts to write there, which the compiler will not allow.

Problem: const variable & string constant 2
Is char a[3] = "ABC" legal? What are the pitfalls of using it?

Answer and analysis:
It's legal in standard C, but it's very narrow; It defines an array of size of 3, initialized to "ABC", and note that it is not the usual string terminator '/ 0, so the array just looks like a string in C language, in essence is not, so all the string handling functions, such as strcpy, printf, etc., can not be used on the fake string.

Question: const & Pointers
Const is used to modify a constant in a type declaration. There are two ways to write const. So, what are the immutable contents of const?

1) const in front
Const int nValue; / / nValue is const
Const char * pContent; //*pContent is const, pContent is variable
Const pContent (char *); //pContent is const,*pContent variable
Char * const pContent; //pContent is const,*pContent variable
Const char * const pContent; //pContent and *pContent are both const

2) const is at the back, which is equivalent to the above declaration
Int const nValue; / / nValue is const
Char * pContent const; // *pContent is const, pContent is variable
(char *) const pContent; //pContent is const,*pContent variable
Char * const pContent; // pContent is const,*pContent variable
Char const * const pContent; // pContent and *pContent are both const

Answer and analysis:
Const and pointer together is a very common confusion in C language, in the actual development, especially when looking at other people's code, it is often difficult to judge the author's intent because of this, the following is my judgment principle:
Draw a line along the * sign, and whoever const is on the side of is const, that is, the element const qualifies is const. You can see the actual meaning of the above statement according to this rule, I believe it will be clear.
Also, note: for const (char *); Because char * is a whole, equivalent to a type (such as char), therefore, this is the qualified pointer to const.


Related articles: