Using the C language const keyword

 

First, conventional usage

 

Const keyword is used to define a read-only variable, the variable is const defined its value is not allowed to change, which does not allow it to be reassigned, even if you can not assign the same value. So it is read-only variables defined, which means that it must give initial value at the time of definition.

 

Const variables with the modified format is usually

 

const type name = value;

 

Code Example; (a first embodiment)

 

const int Max;

 

This can also be written below (second way)

 

int const Max;

 

The first way to use under normal circumstances (const variables will be recommended modification of the first letter capitalized), once they are modified const variable is created and its value can not be changed, it must be assigned constants (initialization) defined at the same time, any behavior behind the assignment will throw an error.

 

Error code examples:

 

Two, const pointer

 

const conjunction with pointer has two functions, one limit pointer variable, two pointer variables to limit data points

 

Restrictions pointer variable itself

 

int * const p2; // const pointer variable is modified

 

Pointer variable restriction means itself, the value of the pointer variable itself can not be modified, so that the pointer variable is a pointer const modified only initialized when defined, then after the assignment can not be defined, the following error code

 

Limit pointer variable data points

 

const int *p1;
int const *p1;

 

Both spelling can be generally used a first, variable limiting sense pointer is pointing to the data pointer may point to different variables (the value of the pointer itself may be modified), but the pointer value can not be modified pointer data, error code show as below

 

Const pointer variable distinction is limited or pointer variables pointing to data values: const variable names from the past is used to modify pointer variables, away from the variable name is used to modify the data pointer, if near and far have, then modify pointer variables and data at the same time it points to.

 

Of course, also be simultaneously limit value of the pointer variable and pointer variable data points, written as

 

const int * const p2;

 

The above wording that the pointer points to a data variable and pointer variable value can not be modified

 

Three, const and Function Parameters

 

In many cases, the modified variables const can use the #define command instead, const commonly used in the function parameter, there are many functions are used const parameter limits in the C standard library, in order to prevent modification within the function pointer to data, e.g. fopen_s

 

Four, const and non-const type of conversion

 

When a pointer variable str1 const is restricted, and similarly const char * str1 this form, a data pointer can not be modified; str1 if not assigned to another modified const pointer variable str2, there may be dangerous .

 

Because data can be modified after the data can not be modified by str1, and assigned by str2, meaning has shifted, so the compiler does not advocate such behavior, it will give an error or warning, as shown below

 

In other words, const char * and char * are of different types, you can not assign const char * data type to a variable of type char *. But the reverse is possible, the compiler allows the assignment of type char * to const char * data types of variables. This limitation is easy to understand, char * data pointed to have read and write permissions, const char * data pointed to only read access privileges to reduce the data will not cause any problems, but the data have elevated privileges It may be dangerous.

 

The above case is that the compiler is permitted

 

Five, const with #define

 

1, define a pre-compiler directives, and const is a general definition of the variable. define macros are defined in the pre-deployment phase, and read-only variable const definition is used in compiling the operational phase.

 

2, const variables are defined, and define constants is defined. define macros defined in the compiler does not exist, it does not take up memory, because it is not a variable, the system will allocate memory to the variable. But the constant variable nature const definition remains a variable,

 

Having basic properties variable, typed, occupies the storage unit. We can say that there is a constant variable name is not variable, but constant no name. There facilitate the name referenced in the program, so that from the point of view to use, in addition to a length of the array can not,

 

Const using constant variables defined macro has the advantage, but also more convenient to use. Therefore, when using a programming const define both the case and possible to make use instead of macro variables constant, but variable constant const can not be replaced in some cases following example will define macros.

 

. 3, const variable is defined, and the macro definition is a constant, so the object const defined data types, and the object is no macro definition data type. So the compiler can type security checks for the former, while the latter is only mechanically character replacement,

 

No type of security check. This makes it easy to go wrong, that is the "marginal issues" or "parentheses issue."

 

Sixth, supplement

 

To define the maximum length of the array, this time can not be used const, const normally defined variable, after all, is a variable, not a length of the array, the following sample code

 

You can only use this case to define defines the maximum length of the array (Here's a comparison of the pit where this happens is the VS2017 compiler will complain, but VC2010 compiler will not complain, it is estimated VC2010 is not so strict)

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159742.htm