const *int、int const *、int *const的区别

- 日理万妓 2022-10-16 04:56 280阅读 0赞
  1. #include<iostream>
  2. using namespace std;
  3. static int test = 1;
  4. const int iNum = 1;
  5. class Test
  6. {
  7. public:
  8. Test();
  9. ~Test();
  10. int const *test_num;
  11. const int *test_num2;
  12. //int *const test_num3;
  13. };
  14. Test::Test()
  15. {
  16. int a = 2;
  17. int bv = 9;
  18. test_num = &a;
  19. test_num2 = &bv;
  20. //test_num3 = &p;
  21. }
  22. Test::~Test()
  23. {
  24. }
  25. int main()
  26. {
  27. Test A;
  28. cout<<*A.test_num<<endl;
  29. cout<<*A.test_num2<<endl;
  30. int iadd = 1;
  31. int *p = &iadd;
  32. const int *ic1 = p;
  33. *p = 90;
  34. cout<<*ic1<<endl;
  35. return 0;
  36. }

20210526234318593.png

const int *跟const *int是一样的,同样修饰的都是int,test_num、test_num2指向一个int型常量的地址,可以改变其指向;

int *const修饰的指针,ic1指向一个常量不能改变其指向;

环境:

Dev-C++ 5.11

编译器:GCC 4.9.2 64-bit

不知道为什么*test_num2的值会是1,它明明指向的是iTmp2的地址,iTmp2值为9呀!

发表评论

表情:
评论列表 (有 0 条评论,280人围观)

还没有评论,来说两句吧...

相关阅读