constants_and_pointers.C
Go to the documentation of this file.
1 #include <iostream>
2 int main()
3 {
4  int const constVar1 =42;
5  const int constVar2 = 13;
6  int variable =11;
7 
8  const int* pointer = &constVar1;//这个const只是告诉编译器,这个指针指向常变量,并没有限制这个指针变量,指针变量是可以变的,如果给这个指针重新定向,这个const并不会限制指针变量
9  std::cout << "the pointer address is " << pointer << std::endl;
10  std::cout << "the pointer points to " << *pointer << std::endl;
11 
12  pointer = &constVar2;
13  std::cout << "the pointer address is " << pointer << std::endl;
14  std::cout << "the pointer points to " << *pointer << std::endl;
15 
16  pointer = &variable;
17  std::cout << "the pointer address is " << pointer << std::endl;
18  std::cout << "the pointer points to " << *pointer << std::endl;
19 
20 
21  int aa =11;
22  int* const constPointer =&aa; //这个const修复的是指针变量,也就是变量是指向固定的地址
23 
24  std::cout << "the pointer points to " << constPointer << std::endl;
25  std::cout << "the pointer points to " << *constPointer << std::endl;
26  aa =99;
27  std::cout << "the pointer points to " << constPointer << std::endl;
28  std::cout << "the pointer points to " << *constPointer << std::endl;
29 
30  return 0;
31 }
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
main
int main()
Definition: constants_and_pointers.C:2