Const keyword
Const=read only, modified as read-only variables instead of constants. A const-modified variable cannot be used as the dimension of an array and cannot be placed after the case of the switch statement:
The main functions are:1. By modifying variables or parameters that do not wish to be modified with const, the compiler will protect these variables from being modified to enhance the reliability of the system;2. Enhance the readability of your code
[HTML]View Plaincopyprint?
- const int A; A is a constant cannot be modified
- int const A; A is a constant cannot be modified
- const int *a; A is a pointer to a constant, the value of a can be variable
- int *const A; The const modifier A,a as a pointer constant to an integer variable
The definition must be initialized for assignment, which cannot be modified once defined. The above code 1, 2, 4 will be compiled with an error indicating uninitialized.
The const and volatile keywords are followed by the type specifier, which acts on the type specifier and, in other cases, the asterisk of the pointer immediately to the left of it.
the difference between typedef and define is embodied in two aspects1.define can extend the name of a defined macro type, and a typedef can not
[CPP]View Plaincopyprint?
- #define PEACH int
- unsigned peach i; //No problem
- typedef int Peach;
- Unsiged Peach i; //error, illegal
The define keyword is just a simple character substitution, and a typedef can be seen as a wrapper over the type, with a new name for the existing type.
2 in the declaration of several consecutive variables only the TypeDef can guarantee the consistency of the type
[CPP]View Plaincopyprint?
- #define INT_PTR int *;
- INT_PTR Chalk,cheese;
Chalk is an int * type, while cheese is an int type. Because define is just a simple string substitution
Special use of typedef
Used to declare a function pointer
[HTML]View Plaincopyprint?
- typedef void (*PTR_FUN) (int);
- Ptr_fun is a function pointer with a parameter of type int and a return value of void
- Ptr_fun sinal (Int,ptr_fun)
- Sinal is a function that accepts arguments of int and ptr_fun two types, with a return value of Ptr_fun