这个关于常量指针的面试题怎么回答?
我接受了一次采访,他们问我这个问题
#include<stdio.h>
int main ()
{
int* const p=NULL;
int const *q=NULL;
p++;
q++;
printf("%d\n",p);
printf("%d\n",q);
}
上述程序将如何表现
a) p 将增加 4 个字节;
q 也会增加 4 个字节;
b) p 将为零
q 将指向前面 4 个字节的内存;
c)上面的程序中会出现错误
我无法理解语句之间的区别
int* const p=NULL;
int const *q=NULL;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
int* const p=NULL;
p
是一个指向整数的常量指针。指针是常量(指针值不能改变);指向的整数不是常量(可以修改整数值)。所以语句:
将无法编译,因为试图修改常量值(指针)。
and 语句:
将增加指针
p
所指向的整数值(但因为p
被赋值为NULL
,所以这将是未定义的行为)int const *q=NULL;
q
是一个指向常量整数的指针。指针不是常量(指针值可以改变);指向的整数是常量(整数值不能修改)。所以语句:
将修改指针
q
以指向前面4个字节的内存(假设sizeof(int)
是4)。 (因为q
被分配了NULL
,q
将是0x4
-- 我假设 NULL 为零(在所有当前实现中都是如此),递增 NULL 指针实际上是未定义的行为)和语句:
将无法编译,因为尝试修改常量值(指向的整数是常量)
int* const p=NULL;
p
is a constant-pointer to an integer. The pointer IS constant (the pointer value cannot be changed); the integer pointed to is not constant (the integer value can be modified).So statement:
will fail to compile because trying to modify a constant value (the pointer).
and statement:
will increment the integer value being pointed by pointer
p
(but becausep
is assignedNULL
, it will be undefined behaviour)int const *q=NULL;
q
is a pointer to a constant-integer.The pointer is not constant (the pointer value can be changed); the integer pointed to IS constant (the integer value cannot be modified).So statement:
will modify pointer
q
to point to memory 4 bytes ahead (assumingsizeof(int)
is 4). (becauseq
is assignedNULL
,q
will be0x4
-- I assume NULL is zero (which is true in all current implementation), incrementing NULL pointer is actually undefined behaviour )and statement:
will fail to compile because trying to modify a constant value (the integer pointed to is a constant)
答案很简单:程序无法编译。
后缀
++
需要一个可修改的左值作为其参数;p
不可修改,因为它是 const 限定的。*
后面的const
表示该指针是合格的;如果const
出现在*
之前,就像在q
的声明中那样,则意味着指针引用的对象是合格的。您可以使用顺时针/螺旋规则解码 C 声明符语法。如果您从程序中删除
p
以及对它的所有引用,以便仅保留包含q
的行,则答案是该程序表现出未定义的行为:您无法对空指针(至少在结果不是空指针时不是)。This is rather simple to answer: the program will not compile.
The postfix
++
requires a modifiable lvalue as its argument;p
is not modifiable because it isconst
-qualified.The
const
after the*
means that the pointer is qualified; if theconst
appears before the*
as it does in the declaration ofq
, it means that the object referred to by the pointer is qualified. You can decode the C declarator syntax using the clockwise/spiral rule.If you remove
p
and all references to it from the program so that only the lines containingq
remain, the answer is that the program exhibits undefined behavior: you cannot perform arithmetic on a null pointer (at least not if the result is not the null pointer).http://publications.gbdirect.co.uk/c_book/chapter8/const_and_volatile.html
他们特别说:
http://publications.gbdirect.co.uk/c_book/chapter8/const_and_volatile.html
They specifically say there:
为了回答这个问题以及更多有关 const 和指针的问题,您必须了解一些基本知识。我将首先口头解释它,然后用一个例子:
指针对象可以声明为 const 指针或指向 const 对象的指针(或两者):
const 指针 不能重新分配给指向到与最初分配的对象不同的对象,但它可用于修改它指向的对象(称为“被指向者”)。因此,引用变量是常量指针的替代语法。
另一方面,指向 const 对象的指针可以重新分配以指向相同类型或可转换类型的另一个对象,但它不能用于修改任何对象。
也可以声明一个指向 const 对象的 const 指针,它既不能用于修改指针对象,也不能重新分配以指向另一个对象。
例子:
For answering this question and many more questions about const and pointers you have to understand something basic. I will explain it verbally first, and then with an example:
A pointer object can be declared as a const pointer or a pointer to a const object (or both):
A const pointer cannot be reassigned to point to a different object from the one it is initially assigned, but it can be used to modify the object that it points to (called the "pointee"). Reference variables are thus an alternate syntax for constpointers.
A pointer to a const object, on the other hand, can be reassigned to point to another object of the same type or of a convertible type, but it cannot be used to modify any object.
A const pointer to a const object can also be declared and can neither be used to modify the pointee nor be reassigned to point to another object.
Example:
我刚刚输入了提供的代码并运行了它,
然后我编译了它,并且只读指针的错误无法修改,在我们的例子中增加如下中止编译>
我只想举一个例子来说明我们不能使用 const 指针。
网络模拟器 ns3 中的另一个示例阐明了常量指针的用法。当我们定义错误模型时将检查到达的数据包。
函数 na
IsCorrupt 返回 true 后(如果出现错误),程序就可以从其数据缓冲区中删除或删除数据包。
请注意,我们不传递 const 指针,从而允许函数在 IsCorrupt() 返回 true 时修改数据包。
I have just put the code provided and ran it,
the
I then compiled it,and an error of read only pointer can not be modified and in our case incremented as below abort compilation
I want just only to give an example of where we can not use const pointers.
Another example from the network simulator ns3 that clarifies constant pointer usage. when we define error model that will examine arriving packets.
a function na
After IsCorrupt return true(in case of error), then the program can then drop or delete the packet from its data buffer.
Note that we do not pass a const pointer, thereby allowing the function to modify the packet if IsCorrupt() returns true.