Daily use of C-language constEvery day, I picked up a C-language shell, which grew up and formed thousands of miles.
Today's shells: C-language const can improve program security and reliability to a certain extent.
1. const modifies the read-only variable: name of the variable of the const Data TypeFor example, const int a = 10;After the const is modified, the variable becomes a read-only variable, which means that the variable must be initialized at the time of definition. Const Read-Only variables, any attempt to modify the read-only variables will cause an error.2. const modifier pointer: as shown in FigureConst int * p; // (* p) (that is, the variable P points to) is immutable, that is, it points to a constant, which is called a constant pointer.Int*Const P; // p is immutable. This pointer is a constant calledPointer constantExample:Constant pointer: Refers to a constant, which can be modified.Pointer constant: A pointer of a constant. The pointer itself cannot be modified.3. const modifier function parameters:When the parameter of a function is a pointer (the parameter is not a pointer and there is no security issue), it is considered as a security concern. If you do not want the function to modify its content, you can use the const modifier to ensure that the function will not modify the parameter.Void foo (const int * p); // The content pointed to by p in the function will not be changed.
Have a nice day!