这个关于常量指针的面试题怎么回答?

发布于 2024-11-16 02:52:26 字数 387 浏览 0 评论 0 原文

我接受了一次采访,他们问我这个问题

#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;

I had an interview in which they had asked me this question

#include<stdio.h>
int main ()
{
int* const p=NULL;
int const *q=NULL;
p++;
q++;
printf("%d\n",p);
printf("%d\n",q);
}

How will above program behave

a) p will increment 4 bytes;
and q will also increment 4 bytes;

b) p will be zero
q will point to memory 4 bytes ahead;

c) error will come in above program

I am not able to understand what is the difference between the statements

int* const p=NULL;
int const *q=NULL;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

内心激荡 2024-11-23 02:52:26

int* const p=NULL;

p 是一个指向整数的常量指针。指针是常量(指针值不能改变);指向的整数不是常量(可以修改整数值)。

所以语句:

p++;

将无法编译,因为试图修改常量值(指针)。

and 语句:

(*p)++;

将增加指针 p 所指向的整数值(但因为 p 被赋值为 NULL,所以这将是未定义的行为)


int const *q=NULL;

q 是一个指向常量整数的指针。指针不是常量(指针值可以改变);指向的整数是常量(整数值不能修改)。

所以语句:

q++;

将修改指针q以指向前面4个字节的内存(假设sizeof(int)是4)。 (因为 q 被分配了 NULLq 将是 0x4 -- 我假设 NULL 为零(在所有当前实现中都是如此),递增 NULL 指针实际上是未定义的行为

和语句:

(*q)++;

将无法编译,因为尝试修改常量值(指向的整数是常量)

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:

p++;

will fail to compile because trying to modify a constant value (the pointer).

and statement:

(*p)++;

will increment the integer value being pointed by pointer p (but because p is assigned NULL, 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:

q++;

will modify pointer q to point to memory 4 bytes ahead (assuming sizeof(int) is 4). (because q is assigned NULL, q will be 0x4 -- I assume NULL is zero (which is true in all current implementation), incrementing NULL pointer is actually undefined behaviour )

and statement:

(*q)++;

will fail to compile because trying to modify a constant value (the integer pointed to is a constant)

寻找我们的幸福 2024-11-23 02:52:26

上面的程序将如何运行?

答案很简单:程序无法编译。

后缀 ++ 需要一个可修改的左值作为其参数; p 不可修改,因为它是 const 限定的。

*后面的const表示该指针是合格的;如果const出现在*之前,就像在q的声明中那样,则意味着指针引用的对象是合格的。您可以使用顺时针/螺旋规则解码 C 声明符语法。


如果您从程序中删除 p 以及对它的所有引用,以便仅保留包含 q 的行,则答案是该程序表现出未定义的行为:您无法对空指针(至少在结果不是空指针时不是)。

How will above program behave?

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 is const-qualified.

The const after the * means that the pointer is qualified; if the const appears before the * as it does in the declaration of q, 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 containing q 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).

吃颗糖壮壮胆 2024-11-23 02:52:26

http://publications.gbdirect.co.uk/c_book/chapter8/const_and_volatile.html

他们特别说:

弹出一个有趣的附加功能
现在。这是什么意思?

字符c; char *const cp = &c;

这真的很简单; cp 是一个指向
一个 char,这正是它想要的
如果 const 不存在的话。这
const 表示 cp 不是
已修改,尽管无论它指出什么
to can be——指针是常量,而不是
它所指向的事物。另一个
方法是

const char *cp;
这意味着现在 cp 是
普通的、可修改的指针,但是
它所指向的东西一定不是
修改的。所以,取决于你做什么
选择要执行的操作,指针和
它指向的东西可能是可以修改的
或不;只需选择合适的
声明。

http://publications.gbdirect.co.uk/c_book/chapter8/const_and_volatile.html

They specifically say there:

An interesting extra feature pops up
now. What does this mean?

char c; char *const cp = &c;

It's simple really; cp is a pointer to
a char, which is exactly what it would
be if the const weren't there. The
const means that cp is not to be
modified, although whatever it points
to can be—the pointer is constant, not
the thing that it points to. The other
way round is

const char *cp;
which means that now cp is an
ordinary, modifiable pointer, but the
thing that it points to must not be
modified. So, depending on what you
choose to do, both the pointer and the
thing it points to may be modifiable
or not; just choose the appropriate
declaration.

谜兔 2024-11-23 02:52:26

为了回答这个问题以及更多有关 const 和指针的问题,您必须了解一些基本知识。我将首先口头解释它,然后用一个例子:

指针对象可以声明为 const 指针或指向 const 对象的指针(或两者):

const 指针 不能重新分配给指向到与最初分配的对象不同的对象,但它可用于修改它指向的对象(称为“被指向者”)。因此,引用变量是常量指针的替代语法。

另一方面,指向 const 对象的指针可以重新分配以指向相同类型或可转换类型的另一个对象,但它不能用于修改任何对象。

也可以声明一个指向 const 对象的 const 指针,它既不能用于修改指针对象,也不能重新分配以指向另一个对象。

例子:

void Foo( int * ptr,
         int const * ptrToConst,
         int * const constPtr,
         int const * const constPtrToConst ) 
{ 
    *ptr = 0; // OK: modifies the "pointee" data 
    ptr = 0; // OK: modifies the pointer 

    *ptrToConst = 0; // Error! Cannot modify the "pointee" data
     ptrToConst = 0; // OK: modifies the pointer 

    *constPtr = 0; // OK: modifies the "pointee" data 
    constPtr = 0; // Error! Cannot modify the pointer 

    *constPtrToConst = 0; // Error! Cannot modify the "pointee" data 
    constPtrToConst = 0; // Error! Cannot modify the pointer 
}

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:

void Foo( int * ptr,
         int const * ptrToConst,
         int * const constPtr,
         int const * const constPtrToConst ) 
{ 
    *ptr = 0; // OK: modifies the "pointee" data 
    ptr = 0; // OK: modifies the pointer 

    *ptrToConst = 0; // Error! Cannot modify the "pointee" data
     ptrToConst = 0; // OK: modifies the pointer 

    *constPtr = 0; // OK: modifies the "pointee" data 
    constPtr = 0; // Error! Cannot modify the pointer 

    *constPtrToConst = 0; // Error! Cannot modify the "pointee" data 
    constPtrToConst = 0; // Error! Cannot modify the pointer 
}
那一片橙海, 2024-11-23 02:52:26

我刚刚输入了提供的代码并运行了它,
在此处输入图像描述
然后我编译了它,并且只读指针的错误无法修改,在我们的例子中增加如下中止编译>
我只想举一个例子来说明我们不能使用 const 指针

网络模拟器 ns3 中的另一个示例阐明了常量指针的用法。当我们定义错误模型时将检查到达的数据包。
函数 na
IsCorrupt 返回 true 后(如果出现错误),程序就可以从其数据缓冲区中删除或删除数据包。

  bool IsCorrupt (Ptr<Packet> pkt);

请注意,我们不传递 const 指针,从而允许函数在 IsCorrupt() 返回 true 时修改数据包。

I have just put the code provided and ran it,
the enter image description here
I then compiled it,and an error of read only pointer can not be modified and in our case incremented as below abort compilationenter image description here
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.

  bool IsCorrupt (Ptr<Packet> pkt);

Note that we do not pass a const pointer, thereby allowing the function to modify the packet if IsCorrupt() returns true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文