Reprint Please specify the source http://blog.csdn.net/xingjiarong/article/details/47282255
Keyword const is used to define constants, if a variable is modified by const, then its value can no longer be changed, I think someone must have this question, C language is not a # define, why also use const, I think the existence of things must have its own truth, So the existence of Const must have its rationality, compared with the pre-compiled instructions, the const modifier has the following advantages:
1. The precompiled instruction simply replaces the value and does not perform type checking
2, can protect the modified things, prevent accidental modification, enhance the robustness of the program
3, the compiler usually does not allocate storage space for ordinary const constants, but instead saves them in the symbol table, which makes it a constant during compilation, without the operation of storage and read memory, which makes it highly efficient.
Let's take a look at the const usage in several ways:
First, modify the local variables
const int n=5;int const n=5;
The two types of writing are the same, it means that the value of the variable n cannot be changed, it should be noted that when the const modifier variable, must be initialized to the face, otherwise you can no longer be assigned.
Next look at the const static string that is used to decorate the constants, for example:
const char* str="fdsafdsa";
If there is no const modification, we may unintentionally write str[4]= ' x ' statements, which will result in the assignment of the read-only memory area, and then the program will terminate abnormally immediately. With const, this error can be checked as soon as the program is compiled, which is the benefit of the Const. Let the logic error be discovered at compile time.
Second, constant pointer and pointer constant
A constant pointer is a constant that the pointer is pointing to, and can be defined in one of two ways.
const int * n;int const * n;
It is important to note that there are two points:
1. Constant pointers say that you cannot change the value of a variable by this pointer, but you can change the value of a variable by another reference.
int a=5;const int* n=&a;a=6;
2, the constant pointer to the value can not be changed, but this does not mean that the pointer itself cannot be changed, the constant pointer can point to other addresses.
int a=5;int b=6;const int* n=&a;n=&b;
A pointer constant means that the pointer itself is a constant and cannot be pointed at another address, as follows:
int *const n;
Note that the address that the pointer constant points to cannot be changed, but the values stored in the address can be changed and can be modified by other pointers to the address change.
int a=5;int *p=&a;int* const n=&a;*p=8;
The key to distinguishing between constant pointers and pointer constants is the position of the asterisk, which is the dividing line for the asterisk, or the constant pointer if the const is to the left of the asterisk, or the pointer constant if the const is to the right of the asterisk. If we read the asterisk as a ' pointer ', and the const is read as ' constant ', the content fits exactly. int const * N; is a constant pointer, int *const n; is a pointer constant.
Regular pointer to constant
Is the combination of the above two, the position of the pointer can not be changed and can not change the value of the variable through the pointer, but still be able to change the value of the variable by other ordinary pointers.
const int* const p;
Iii. parameters of the modifier function
According to constant pointers and pointer constants, the parameters of the const modifier function are also divided into three cases
1. Prevent changes to the content that the pointer points to
void StringCopy(char *strDestination, const char *strSource);
Where strsource is the input parameter, strdestination is the output parameter. After adding a const modifier to strsource, the compiler will indicate an error if the statement in the function body attempts to alter the contents of the strsource.
2. Prevent modifying the address pointed to by the pointer
void swap ( int * const p1 , int * const p2 )
Neither the pointer P1 nor the address pointed to by the pointer P2 can be modified.
3, the combination of the above two.
Iv. The return value of the modifier function
The contents of a function return value (that is, the pointer) cannot be modified if the return value of the function returned by the "pointer Pass" method is added to the const modifier, and the return value can only be assigned to a const-decorated pointer of the same type.
such as functions
const char * GetString(void);
A compilation error will appear in the following statement:
char *str = GetString();
The correct usage is
const char *str = GetString();
Five, modify the global variables
The scope of the global variable is the entire file, we should try to avoid using global variables, that once a function changes the value of the global variable, it will also affect other references to this 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, which is not necessary to modify, and the method used is the same as the local variable.
Copyright NOTICE: This article for Bo Master original article, reproduced please indicate the source, view the original article, please visit: Http://blog.csdn.net/xingjiarong
Usage of the Const keyword in c language