Maybe there are still many O N S T Repair Decoration The const modifier is used only when reading other people's code, I have never used my own code writing, so it is natural for C O N S T Repair Decoration Character is unfamiliar. So what is C? O N S T Repair Decoration How to effectively use C O N S T Repair Decoration Now let's learn about C. O N S T Repair Decoration .
Const is a relatively new descriptor in C language. We call it a constant modifier, that is, the object it modifies is a constant. If you want to prevent a variable from being changed in your code, you can use the const keyword. When you add a const modifier to a variable, you usually need to initialize it, and you cannot change it in subsequent programs.
Some people may have a question. We don't have a pre-processing command in C # define variablename variablevalue can be easily used for value substitution. Why should we introduce the const modifier ?! This is because the pre-processing statement can be easily replaced, but it has a fatal disadvantage, that is, the pre-processing statement is only a simple value replacement, and lacks a type detection mechanism. In this way, the pre-processing statement cannot enjoy the strict type check of the C compiler. It is precisely because of this that there are a series of hidden dangers and limitations in its use.
Before explaining the const modifier, we will first give several typical functions of the const modifier:
1. Const Type Definition: indicates that the value of a variable or object cannot be updated. It is introduced to replace the pre-compiled command.
2. It can protect modified items, prevent accidental modifications, and enhance program robustness;
3. generally, the compiler does not allocate storage space for common const constants, but stores them in the symbol table, which makes it a constant during compilation without the operation of storage and read memory, this makes it highly efficient.
4. It can save space and avoid unnecessary memory allocation.
Next, let's take a look at the specific usage.
1. the const modifier modifies local variables in the function.
Const int n = 5;
And
Int const n = 5;
Is equivalent. In the programming process, we must clearly know who the const object is. Here we modify N, which has nothing to do with Int. Const requires that the object to be modified be a constant, cannot be changed, and cannot be assigned a value. Therefore, the following statement is incorrect.
Const int N;
N = 0;
It is easy to understand the above situation, but it is easy to get confused when the const is used with the pointer. For example, let's look at a Statement of p and q:
Const int * P;
Int const * q;
After reading the code above, some people may think that const int * P; represents a pointer of the const int type (const directly modifies INT), while int const * q; indicates a const pointer of the int type (const directly modifies the pointer ). In fact, in the above declaration, both p and q are declared as const int type pointers. The const pointer of the int type should be declared as follows:
Int * const r = & N;
The above p and q are pointers to the const int type. That is to say, you cannot change the * P value in future programs. R is a const pointer. After it is initialized to the variable N (that is, r = & N;) during the declaration, the value of R is no longer allowed to be changed, however, the value of * r can be changed. Here, we provide a common method for determining the const modifier object. We take the * as the boundary. If the const is located on the left side of *, the const is used to modify the variable pointed to by the pointer, that is, the pointer points to a constant. If the const is on the right side of *, const modifies the pointer itself, that is, the pointer itself is a constant.
Let's give a code to help you better understand it.
# Include <stdio. h>
Int main (INT argc, char * argv [])
{
Int Ss = 9;
Int * const r = & SS;
Printf ("% d \ n", * R );
Printf ("% d \ n", SS );
* R = 100;
Printf ("% d \ n", * R );
Printf ("% d \ n", SS );
Return 0;
}
The running result is as follows:
Let's analyze it simply. Because R points to the SS address, the value of the address Unit pointed to by r changes with the SS value.
Based on the above two const modifiers, we should be able to declare a const pointer pointing to the const int type as follows:
Const int * const r = & SS;
In this case, neither the * r value nor the R value can be modified.
Next, let's take a look at the constant static string that const is used to modify, for example:
Const char * STR = "fdsafdsa ";
Without the const modifier, we may intentionally or unintentionally write a statement like STR [4] = 'X' later, which will lead to a value assignment to the read-only memory area, then the program will terminate unexpectedly immediately. With const, this error can be checked immediately when the program is compiled. This is the benefit of Const. Make logical errors discovered during compilation.
2. Const modifies parameters during function declaration
Void * memmove (void * DEST, const void * SRC, size_t count); this is a function in the standard library, in the header file # include <string. h> indicates that the function is to copy count bytes from the memory area indicated by Src to the memory area indicated by DeST. Used to copy strings (memory) in bytes ). Its first parameter is to copy the string to the destination (DST), which must be writable in the memory area. Its second parameter is to copy out a string. We only read and write data for this memory area. Therefore, from the perspective of the function itself, the data stored in the memory to which the SRC Pointer Points remains unchanged throughout the function execution process. So SRC points to a constant. Therefore, you need to use the const modifier. In addition, the SRC and DEST memory regions can overlap, but the Dest content will be changed after replication. The function returns a pointer to DeST.
For example, we use it here.
# Include <stdio. h>
# Include <string. h>
Int main (INT argc, char * argv [])
{
Const char * STR = "hello ";
Char Buf [10];
Memmove (BUF, STR, 6 );
Printf ("% s \ n", Buf );
Return 0;
}
The running result is as follows:
If we write memmove (STR, Buf, 6) in turn, the compiler will certainly report an error. In fact, we often reverse the Parameter order of various functions. The fact is that the compiler has helped us a lot at this time. If the compiler quietly does not report an error, that is, in the function declaration void * memmove (void * DEST, const void * SRC, size_t count); remove the const, this program will crash when running. It is also worth noting that in the function parameter declaration, const is generally used to declare the pointer rather than the variable itself. For example, the above size_t Len does not need to change the Len value during function implementation. Should Len be declared as a constant? Yes, you can. Let's analyze the advantages and disadvantages of doing so. If const is added, the implementer of this function can prevent him from modifying the value (LEN) that does not need to be modified when implementing this function. This is good. But for the user of this function,
1. The decoration symbol is meaningless. We can pass a constant integer or an extraordinary integer to the past. The other party obtains only a copy we pass.
2. I don't need to know if you have modified the Len value when implementing this function.
Therefore, const is generally only used to modify pointers. Let's look at a complicated example.
Int execv (const char * path, char * const argv []);
Focus on what argv stands for later. If you remove const, we can see that char * argv [], argv is an array, and each element of it is a pointer of the char * type. If Const. is added, who is the const modifier? Modifies an array. argv [] indicates that the elements of this array are read-only. What is the element type of the array? Char * type
Needle. That is to say, the pointer is a constant, and it points to no data. Therefore
Argv [1] = NULL; // invalid
Argv [0] [0] = 'a'; // valid
3. Use const as a global variable
In the program, we need to use as few global variables as possible. Because its scope is global, you can modify its value within the scope of the program. As a result, global variables cannot guarantee the correctness of the value. If an error occurs, it is very difficult to find out. If you use global variables in multiple threads, your program will be messy. Multithreading modifies the value of the global variable used by another thread. If you do not pay attention to it, the consequence of an error is unimaginable. In this case, do not use global variables. We need to use const as much as possible. If a global variable is used only in this file, its usage is no different from the local variable of the function mentioned above. If it is to be shared among multiple files, it involves a storage type issue.
There are two methods.
1. Use extern
For example
/* Pi. H */
Extern const double PI;
/* Pi. C */
Const double Pi = 3.14;
Other variables that need to use PI include PI. h.
# Include PI. h
Or copy the statement. The result is that after the entire program is linked, all the storage areas that need to use the PI variable are shared.
2. Use static and static External Storage Class
/* Constant. H */
Static const double Pi = 3.14;
The *. c file that needs to use this variable must contain this header file. The preceding static values must not be small. Otherwise, the variable is defined multiple times. The result is that every one contains constant. H *. in the c file, there is a copy of the variable. The variable is actually defined multiple times and occupies Multiple Buckets. However, after the static keyword is added, the file definition conflict is solved. The disadvantage is that the storage space is wasted, resulting in larger executable files after the link. Generally, it is not a problem if the size of the bucket is not too large. The advantage is that you don't have to worry about the file in which the variable is initialized.
Let's take a look at the code below:
# Include <stdio. h>
Int main ()
{
Const int A = 12;
Const int * P = & A; // This is a pointer to a constant. The Pointer Points to a constant.
P ++; // you can add or subtract pointers
P --; // valid
Int const * q = & A; // This is the same as the preceding const int * P = &;
Int B = 12;
Int * const r = & B; // This is a constant pointer (constant pointer). It cannot be auto-incremented or auto-subtracted and must be initialized.
// R ++; // compilation Error
Const int * const T = & B; // This is a constant pointer to a constant and needs to be initialized with a variable
// T ++; // compilation Error
P = & B; // The const pointer can point to const and non-const objects.
Q = & B; // valid
Return 0;
}
In order to facilitate the reading and understanding of the code, I will add comments directly after the code, so I will not explain it too much here.
To sum up, the advantage of const is to introduce the concept of constants so that we do not need to modify the memory that should not be modified. The direct function is to enable more logical errors to be detected during compilation. Therefore, we should try to use const as much as possible. The above content is inevitable. If it is incorrect, please correct it! If you need to reprint it, please indicate the source!
I would like to share some gossip here. I accidentally found some websites and individuals in Google search a few days ago to repost my articles without my consent without marking any source information, A netizen posted an article in another forum as the first author without marking the source of any article. I hope that while reprinting the article can also mark the source, after all, it is also a little respect for the original at least, I hope we all have a sense of respect for the original, my blog address: http://blog.csdn.net/bigloomy