1. Replace values with values in C Language Preprocessor replace names: # define BUFSIZE 100C ++ replace values with const into the compiler field: const bufsize = 100; or const int bufsize = 100; you can also execute constant folding: char buf [bufsize]; 1.1 the const in the header file is the same as that in # define. To use const, you must put the const definition in the header file. In this way, the const definition can be put in a separate place by including the header file and allocated to a compilation unit. By default, const is an internal connection, that is, it is visible only in the file defined by const, and cannot be seen by other compilation units during connection. For example, in Test. as defined in h, const int nTestConst = 1000; in. include "Test. h "at the same time in B. include "Test. h "does not appear. If it is not defined as const, the dependent variable definition has two errors. when defining a const constant, a value must be assigned to it unless extern is clearly described: extern const bufsize; although the above extern forces the bucket allocation, however, the C ++ compiler does not allocate storage space for const. Instead, it stores the definition in its symbol table. When a const is used, it performs constant folding during compilation. 1.2 const security if you want to keep a value unchanged, you should make it a constant const ). 1.3 The const of a set can be used for a set, but memory must be allocated, which means "a block of storage that cannot be changed ". However, the value cannot be used during compilation. Const int I [] = {1, 2, 3, 4}; // float f [I [3]; // Illegal2, when a const with a pointer is used, there are two options: 1. Or const modifies the object to which the pointer is directed, 2. const modifies the address stored in the pointer itself. 2.1 The pointer pointing to const uses the pointer definition technique. Just like any complicated definition, it reads the identifier at the beginning and reads it from the inside out. If we want to keep the positive element unchanged, write: const int * x; Starting from the identifier: "x is a pointer and points to a const int ." Moving const from one side to the other has the same result. Int const * x; 2.2 The cosnt pointer is the pointer itself becoming a const pointer. You must place the part indicated by const on the Right of *, for example, int d = 1; int * const x = & d; the pointer itself is a const pointer. The Compiler requires an initialization value, which remains unchanged during the lifetime of the pointer. However, to change the value it points to, you can write * x = 2. These can be summarized into the following sentence: When the const is on the left side of *, the actual object value remains unchanged. When the const is on the right side of *, the pointer remains unchanged, that is, it is easier to remember the left object and right object !! |