const 指针和普通指针可以混合吗?
调用 printf() 时 *b 的值是否未定义?
void foo(int *a) {
const int *b = a;
int *c = a;
*c = 2;
printf("%d\n", *b); // what must be *b? 1, 2 or undefined?
}
int d = 1;
foo(&d);
Is the value of *b undefined when printf() is called?
void foo(int *a) {
const int *b = a;
int *c = a;
*c = 2;
printf("%d\n", *b); // what must be *b? 1, 2 or undefined?
}
int d = 1;
foo(&d);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它将打印 2。
const int *b
字面意思是:这并不意味着指针指向的值可能不会改变。事实上,改变是完全正确的。使用此功能的一个可能场景是保留对某些大型结构的只读引用的结构。引用可能会改变,但使用该结构的函数可能不会改变指针后面的内容。
想象一个驱动程序或类似的驱动程序,它分发设备传递的任何数据的只读内存映射:映射的地址不是恒定的,但由于这是只读映射,用户程序可能无法写入它。 OTOH 当设备更新数据时,缓冲区的内容会改变,但映射地址不一定会改变。
It will print 2.
const int *b
literally means:This does not mean that the value the pointer points to may not change. In fact it's perfectly valid to change. A likely scenario to use this, are structures that keep a read only reference to some large structure. The reference may change, but the functions working with that structure may not change what's behind the pointer.
Imagine a driver or similar that hands out a read only memory mapping of whatever data the device delivered: The address of the mapping is not constant, but since this is a read only mapping the user program may not write to it. OTOH when the device updates the data the contents of the buffer will change, but not necessarily the mapping address.
标准所说的是(重点是我的)
这不适用于您的情况(恰恰相反)。
相关对象是用普通 (
int
) 类型定义的。在您的情况下,只有通过
b
对对象进行的更改是非法的;通过a
或c
进行的更改是完全合法的What the Standard says is (emphasis is mine)
This does not apply to your situation (just the other way around).
The object in question was defined with a plain (
int
) type.In your situation only changes to the object through
b
are illegal; changes througha
orc
are perfectly legal由于
b
与a
指向相同的内存,所以该值当然会改变。不知道为什么你还引入了c
,但它没有添加任何东西。它将打印2
。Since
b
points at the same memory asa
, the value will of course change. Not sure why you also introducedc
though, but it doesn't add anything. It will print2
.*b
将为 2,因为设置该值的printf
之前的最后一行是*c = 2
。a
、b
和c
都指向相同的整数值。所以最后一个设置它的将决定它的当前值。*b
would be 2 because the last line before theprintf
that sets the value is*c = 2
.a
,b
, andc
all point to the same integer value. So the last one to set it will determine its current value.printf() 中的 b 应为 2。您始终在 b 和 c 中处理指针。
b should be 2 in printf(). You handle a pointer all the time in b and c.
声明
const int *b = a;
表示b
引用常量 int 值。也就是说它把它的值当作一个常数值。因此,
是不正确的,但是:
很好,因为 a 不是常量值,但是当取消引用 b 时,我们将其视为常量。
所以
b
肯定是定义的,因为您更改了它指向的值:这与上面的示例相同。简而言之,指向 const 值的指针不能通过取消引用来修改该值,但可以通过其他方式更改它指向的值。
The declaration
const int *b = a;
means thatb
refers to a constant int value. Which is to say that it treats its value as a constant value.Thus,
is incorrect, but:
is fine since a is not a constant value, but when dereferencing b we treat it as constant.
So
b
is certainly defined since you changed the value it pointed to:which is the same as the example above. Simply put, a pointer to a const value cannot modify the value through dereferencing, but the value which it points to can otherwise be changed.