"0" Overview
Const is the abbreviation for constant, one of the keywords of the C language, and is a type modifier (type specifier). Const is a constant meaning that is also translated into constants, constants, and so on. Unfortunately, it is because of this that many people think that the value of the const modifier is constant. This is imprecise, and should be precise, a read-only variable whose value cannot be used at compile time because the compiler does not know what it is storing at compile time. Perhaps this keyword should have been replaced with ReadOnly. So what is the use and meaning of this keyword?
It restricts variables to read-only variables, that is, variables are stored in read-only static storage . At compile time, how to try to modify the read-only variable, the compiler prompt error, can prevent false modification . The use of const can improve the security and reliability of the program to some extent. The initial purpose of the const rollout is to replace the precompiled instruction, eliminate its drawbacks, and inherit its advantages.
"1" effect1.1 Const-Modified read-only variable
For example:
1 Const int max=; 2 int Array[max];
Here, you should create a. C file in Visual c++6.0 and test it separately. You will find that in the. c file, the compiler prompts for an error, which is also confirmed by the side in the C language, the const-decorated Max is still a variable, just a read-only property.
1.2 Save space, avoid unnecessary memory allocations, and increase efficiency
The compiler typically does not allocate storage space for ordinary const read-only variables, but instead saves them in the symbol table, which makes it a compile-time value, without the memory and read operations, making it highly efficient.
For example:
1 #defineM 3//Macro Constants2 Const intn=5;//N is not put in memory at this time3 ......4 intI=n;//allocate memory for N at this time, no longer assigned! 5 intI=m;//macro substitution during precompilation, allocating memory6 intJ=n;//No memory Allocations7 intJ=m;//Another macro replacement, another memory allocation!
Const-defined read-only variable from the assembly's point of view, just give the corresponding memory address, instead of the immediate number as given in # define, so the const definition of the read-only variable in the program run only one copy (because it is a global read-only variable, stored in the static area), and # The macro constants defined by define have several copies in memory. #define The macro is replaced during the precompilation phase, and the const-modified read-only variable determines its value at compile time. #define A macro has no type, and a const-decorated read-only variable has a specific type.
1.3 Modifying General variables
A general constant is a read-only variable of a simple type. When this read-only variable is defined, the modifier const can be used in front of the type descriptor, or it can be used in the type description specifier. For example:
1 int Const i=2; 2 // or 3 Const int i=2;
1.4, modifying arrays
Define or describe a reading group that can be in the following format:
1 int Const a[5]={12345}; 2 // or 3 Const int a[5]={12345};
1.5, modifier pointer
1 Const int *p; // p variable, the object that P points to is immutable 2 int const *p; // p variable, the object that P points to is immutable 3 INT *const p; // P is immutable, the object to which P points is variable 4 Const INT *const// pointer p and P point object are immutable
Give a way of remembering and understanding:
Ignore the type name first (compiler parsing is also ignore the type name), we look at the const from which near. "Tun", who is near to decorate who.
1 Const int*p;//The const modifier *P,P is a pointer, *p is the object that the pointer points to, immutable2 int Const*p;//The const modifier *P,P is a pointer, *p is the object that the pointer points to, immutable3 int*ConstP//Const modifier p,p immutable, P-pointed object variable4 Const int*ConstP//The previous const modifier *P, the latter a const modifier p, the pointer p and P point to the object are immutable
const INT *a
Here the const modifier is int, and int defines an integer value
Therefore, the object value pointed to by *a cannot be modified by *a, but can be assigned to a to point to a different object
eg
1 Const int*a =0;2 Const intb =1;3 intc =1;4A = &b//ok! Extra: Note that the B value cannot be modified by a5A = &c//ok! Extra: Although C itself is not a constant6*a =2 //erro! In this case, you cannot modify the value of the object pointed to by *a, and the last assignment is C, so you cannot modify the C value by *a.
int const *A
And the meaning of the const int *A is the same as the effect of their two equivalents
int *const A
The const modifier Here is a, a is a pointer address and therefore cannot be assigned to a other address value, but you can modify the value of a to point to the opposite meaning of cont int *a
const INT * Const A
This represents the value of the object to which a is pointing and the address itself cannot be changed.
1.6, parameters of the modifier function
The const modifier can also modify the parameters of a function so that it does not want the parameter value to be changed unexpectedly by the function body.
Use. For example:
void Fun (constint i);
Tells the compiler I cannot change in the body of the function, thus preventing some unintentional or erroneous modifications by the user.
1.7, the return value of the modifier function
The const modifier can also modify the return value of the function, and the return value cannot be changed. For example:
Const int Fun (void);
To reference a const read-only variable in another connection file:
extern Const int i; // the right statement extern Const int j=// Error! The value of a read-only variable cannot be changed.
"3" Q&a
Q:
Const with the The difference between define ?
A:
Both can be used to define constants, but when const is defined, the type of the constant is defined, so it is more accurate (in fact, the const defines a read-only variable, not a constant). #define is just a simple text substitution, in addition to defining constants, it can be used to define simple functions that are somewhat similar to built-in functions. constants defined by const and define can be placed inside the header file. (Small bet: can be declared more than once, but only once)
A few additions to the const:
1. The address of a const object can only be assigned a pointer to a const object
2. A pointer to a const object can be assigned to an address of a non-const object
3. A pointer to const is often used as a form parameter of a function, guaranteeing that the actual object passed to the function will not be modified in the function until the actual object is
4. Constants cannot be modified after they are defined, so it must be initialized. Uninitialized constant definitions will result in compilation errors (all of which are explained by the const problem, so there is no assignment, the actual statement is to be assigned value)
"C Basic Knowledge" Const knowledge Encyclopedia