Keyword const is used to define constants, if a variable is modified by the const, then its value can not be changed again, I think someone must have such a question, C language is not a #define, why do you have to use the const, I think the existence of things must have its own truth, Therefore, the existence of the const must have its rationality, compared with the precompiled instructions, theconst modifier has the following advantages :
1, the precompiled instruction is only a simple replacement of the value, can not be type check
2, can protect the modified things, prevent accidental modification, enhance the robustness of the program
3. Compilers usually do not allocate storage space for ordinary const constants, but instead store them in the symbol table, which makes it a constant during compilation, without the operation of storing and reading memory, making it efficient.
Here are a few ways to use the const:
Modification of local variables
const int n=5;
int const n=5;
These two kinds of writing is the same, is to indicate that the value of the variable n can not be changed, it should be noted that the use of const modifier variable, must be initialized to the face, otherwise it can no longer be assigned.
Next look at the const for modifying constant static strings, for example:
Const char* str= "FDSAFDSA";
Without a const modifier, we might write a statement str[4]= ' X ' in the back, which would result in an assignment to the read-only memory area, and then the program would terminate abnormally immediately. With the const, this error can be checked out as soon as the program is compiled, which is the benefit of Const. Let logical errors be found at compile time.
Two, constant pointers and pointer constants
A constant pointer is a constant that the pointer points to, and there are two ways to define it.
const int * n;
int const * N;
What you need to be aware of is two points:
1. The constant pointer says that you cannot change the value of a variable through this pointer, but you can change the value of a variable by using another reference.
int a=5;
Const int* n=&a;
a=6;
2. The value that the constant pointer points to cannot be changed, but this does not mean that the pointer itself cannot be changed, and the constant pointer can point to another address.
int a=5;
int b=6;
Const int* n=&a;
n=&b;
A pointer constant is a constant in the pointer itself and cannot point to another address, as follows:
int *const N;
Notice that the pointer constant points to an address that cannot be changed, but that the values stored in the address can be changed, and can be modified by other pointers to the change of address.
int a=5;
int *p=&a;
int* Const n=&a;
*p=8;
The key to distinguishing between constant and pointer constants lies in the position of the asterisk, which is the line of the asterisk, and if the const is to the left of the asterisk, a constant pointer, or a pointer constant if the const is to the right of the asterisk. If we read the asterisk as a ' pointer ', and read the const as ' constant ', the content fits exactly. int const * N; is a constant pointer, int *const n; is a pointer constant.
Constant pointers to constants
Is the combination of the two, the position of the pointer cannot be changed and can not change the value of the variable through the pointer, but can still change the value of the variable through other normal pointers.
const int* const p;
iii. parameters of the modifier function
Depending on the constant pointer and the pointer constant, the parameters of the const modifier function are divided into three different cases
1, to prevent changes to the contents of the pointer
void Stringcopy (char *strdestination, const char *strsource);
Where strsource is an input parameter, strdestination is an output parameter. After adding a const modifier to the strsource, the compiler will point out an error if the statement in the body of the function attempts to change the contents of the strsource.
2, to prevent the modification of the address of the pointer
void Swap (int * const P1, int * const p2)
Both the pointer p1 and the pointer p2 the address you are pointing to cannot be modified.
3, the above two kinds of combination.
Four, the return value of the modifier function
If you modify the function return value with the "pointer Pass" method, the contents of the function return value (that is, the pointer) cannot be modified, and the return value can only be assigned to the same type of pointer with the const modifier.
such as functions
const char * GetString (void);
The following statement will present a compilation error:
Char *str = GetString ();
The correct usage is
const char *STR = GetString ();
v. Modifying global variables
Global variable scope is the entire file, we should try to avoid the use of global variables, that once a function changes the value of the global variable, it will also affect other reference to the variable function, resulting in addition to the bug is difficult to find, if you must use global variables, We should try to decorate with the const modifier as much as possible, so that it is not necessary to modify the method in the same way as the local variable.
The above is the full use of the Const keyword to help you use the Const keyword flexibly