C ++ Programming Thoughts Reading Notes - Const

xiaoxiao2021-03-06  41

C const

Const is used to define constants in standard C and C design. At first, it is considered to use const instead of the #define macro to travel in C . There are two main functions: 1, value replace, 2, pointer replacement

Text instead

#define is a text instead of pre-processing, no storage space, no type check const can define types, accounting for storage space const energy = 100; char sz [buffsize]; used for collection

Const can be used in a collection, but the value in the collection cannot use const I [] = {1, 2, 3, 4}; char SZ [i [1]]; // illegal pointer during compilation

Pointer to Const

Const modified pointer is pointing to the object const * x; const INT * x; (x is a pointer, it pointing to a const INT) X point to the value cannot be changed, but the address of the X can change

INT D1 = 1; const * x = & d1; x = & d1; // legal INT D1 = 1; const * x = & d1; * x = D1; // illegal const pointer

Const modification is stored in the address of the pointer itself INT * const x; int * const x = & d; (x is a pointer, this pointer is pointing to the consT pointer) compiler requires an initial value, X The address cannot be changed, but the point to the point can be changed.

INT D1 = 1; int * const x = & d1; * x = D1; // legal INT D1 = 1; int * const x = & d1; x = & d1; // illegal

转载请注明原文地址:https://www.9cbs.com/read-71249.html

New Post(0)