静态指针 const

int *const
const int*
它们的区别,以及const int *const的区别

// 2011 3 3
#include<iostream>
using namespace std;

int main()
{
int b = 500,b1 = 200;
const int *a = &b;
cout << "&b= " << &b << endl;
cout << "a= " << a << endl;
cout << "*a= " << *a << endl;
a = &b1;
cout << "&b1= " << &b1 << endl;
cout << "a= " << a << endl;
cout << "*a= " << *a << endl;
//*a = 3;
int const *a1 = &b;
cout << "a1= " << a1 << endl;
a1 = &b1;
cout << "a1= " << a1 << endl;
//*a1 = 4; //左变量 右指针
int * const c = &b;
cout << "c= " << c << endl;
cout << "*c= " << *c << endl;
//c = &b1;
*c = 4;
cout << "c= " << c << endl;
cout << "*c= " << *c << endl;

const int * const d = &b;
cout << "d= " << d << endl;
cout << "*d= " << *d << endl;
//d = &b1;
//*d = 5;
return 0;
}




一 const基础

如果const关键字不涉及到指针,我们很好理解,下面是涉及到指针的情况:
int b = 500;
const int* a = &b; [1]
int const *a = &b;[2]
int* const a = &b;[3]
const int* const a = &b;[4]

1.如果const位于星号的左侧,则const就是用来修饰指针所指向的变量,即指针指向为常量;如果const位于星号的右侧,const就是修饰指针本身,即指针本身是常量。因此,[1]和[2]的情况相同,都是指针所指向的内容为常量(const放在变量声明符的位置无关),这种情况下不允许对内容进行更改操作,如不能*a = 3 ;
2.[3]为指针本身是常量,而指针所指向的内容不是常量,这种情况下不能对指针本身进行更改操作,如a++是错误的;
3.[4]为指针本身和指向的内容均为常。

总结如下:

const int* a = &b; 相当于:const int (*a) = &b; 这个静态指的是(int *a) 即指针指向的变量为静态;

int const *a = &b; 相当于:int const (*a) = &b; 同样是指向变量为静态;

int* const a = &b; 相当于:int (*const a) = &b; 指针为静态,但是指针指向的变量值可以改变;

const int* const a = &b; 指针和指针指向的变量都为静态;

2011 4 25 补充

#include <iostream>
using namespace std;

int main()
{
int age = 39;
int *ptt = &age;
const int *pt = &age;
*ptt += 1;
cout << *pt <<endl << age << endl;
return 0;
}

输出:40
40
const int *pt = &age;
Our declaration for pt doesn't necessarily mean that the value it points to is really a constant; it just means the value is a constant insofar as pt is concerned.
For example, pt points to age, and age is not const. You can change the value of age directly by using the age variable, but you can't change the value indirectly via the pt pointer

也就是说在利用const int *pt = &age;时,age的copy副本是一个静态量,但age本身并不是静态量,age本身的值可以改变,但是不能通过指针pt改变age的值,因为通过指针pt改变的值是静态的age副本,所以不可改变。

That leaves two other possibilities: assigning the address of a const variable to a pointer-to-const and assigning the address of a const to a regular pointer. Are they both possible? The first is, and the second isn't:

const float g_earth = 9.80;
const float * pe = &g_earth; // VALID

const float g_moon = 1.63;
float * pm = &g_moon; // INVALID


For the first case, you can use neither g_earth nor pe to change the value 9.80. C++ doesn't allow the second case for a simple reason—if you can assign the address of g_moon to pm, then you can cheat and use pm to alter the value of g_moon. That makes a mockery of g_moon's const status, so C++ prohibits you from assigning the address of a const to a non-const pointer.


What does the expression *"pizza" mean? What about "taco"[2]?

A: Because C++ interprets "pizza" as the address of its first element, applying the * operator yields the value of that first element, which is the character p. Because C++ interprets "taco" as the address of its first element, it interprets "taco"[2] as the value of the element two positions down the line, that is, as the character c. In other words, the string constant acts the same as an array name.
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值