CONST use in C language

xiaoxiao2021-03-06  78

Const use in C

Const is a keyword of a C language that defines a variable that is not allowed to be changed. Using const can improve the security and reliability of the program to a certain extent.

Const's use is a subtle place in the C language, please see the following questions.

Problem: const variables and constants

Why is the following example to initialize an array using a const variable, will ANSI C report a bug?

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, for example 5, "abc", etc., must be read-only because constant is the read-only area where the compiler is placed in memory, 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. And ANSI C specifies the length of the array to define the length of "constant" and "read-only variable" is also.

2) Note: In ANSI C, this way of writing is wrong because the size of the array should be a constant, and const INT N, N is just a variable (constant! = Non-variable variable, but in standard C , this definition Is a constant, this way is right), in fact, in accordance with the compilation process and memory allocation, this usage should be reasonable, just that the ANSI C pairs the array limit it.

3) So, 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 and constant 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 for use in 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 CHAR, Const NewType M;

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

Problem: const variables and string constants

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.

Problem: const variables and string constants 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 narrow; it defines an array of size 3, which is initialized to "ABC", but it does not have a usual string terminator '\ 0', so this array is just It 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 and pointers

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 const

Const 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, the above statement is right

Int const nvalue; // nvalue is const

Char const * pcontent; // * PContent is const, PContent 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 constant

Answer and analysis:

CONST and pointers are used in C language a very common confusion in the actual development, especially when seeing others code, often because of this, not judge the author's intentions, hereby introducing an effective judgment in principle:

A, when there is "()", for example

Const (char *) PCONTENT;

At this time, (char *) is a whole, equivalent to a new type, therefore, then the defined pointer is const.

B, other cases

l On the * number, if the const is on the left side of *, the const is used to modify the variable pointed to by the pointer, that is, the constant refers to the pointer;

l If the const is on the right side of the *, const is the modified pointer itself, that is, the pointer itself is constant.

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

New Post(0)