究竟是什么" const int的* PTR =安培; I"是指非常数?为什么接受地址? [英] What exactly does "const int *ptr=&i" mean?Why is it accepting addresses of non-constants?

查看:434
本文介绍了究竟是什么" const int的* PTR =安培; I"是指非常数?为什么接受地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您答案是非常抢手以清除这一重大空白在我的理解关于常量我今天实现。

Your answers are very much sought to clear this major lacuna in my understanding about const that I realized today.

在我的节目,我已经使用了语句 const int的* PTR =放;我; ,但没有使用任何常量预选赛变量。两个东西都困惑我:

In my program I have used the statement const int *ptr=&i; but haven't used any const qualifier for the variable i.Two things are confusing me:

1):当我尝试修改的值使用 PTR ,在这里我使用了 const int的* PTR =放;我; ,我得到的错误只读位置分配'* PTR'| ,虽然我还没有声明的变量常量 qualifier.So什么究竟的声明 const int的* PTR =放;我; 的意思,它是如何从 INT不同* const的PTR =放;我;

1) When I try to modify the value of i using ptr ,where I have used const int *ptr=&i;,I get the error assignment of read-only location '*ptr'|,even though I haven't declared the variable i with the const qualifier.So what exactly the statement const int *ptr=&i; mean and how does it differ from int * const ptr=&i;?

我有它钻到我的头说 const int的* PTR =放;我; 表示指针存储常量的地址,而为int * const的PTR =放;我; 表示指针本身是恒定的,今天一个用户告诉我,在讨论不能change.But(<一个href=\"http://stackoverflow.com/questions/16535795/please-look-into-this-inexplicable-behavior-and-output-of-memcpy-for-overlappi/16535852#16535852\">LINK)该 const int的* PTR是指指向的内存必须被视为不可改变_through这个指针_ 。我发现这个新的东西,因为这意味着还挺一些选择指针不能改变值(而其他人可以)。我不知道这种有选择的声明!!但一个180K的老将证明了它的用户是正确的!所以,你可以在一个更清晰,更详细,更严格的方式说明这一点?究竟是什么`const int的* PTR =放;我; `意味着

I had it drilled into my head that const int *ptr=&i; means that the pointer stores the address of a constant,while int * const ptr=&i; means the pointer is itself constant and can't change.But today one user told me in discussion(LINK) that const int *ptr means the memory pointed to must be treated as nonmodifiable _through this pointer_.I find this something new as this kinda means "some select pointer can't alter the value(while others can)".I wasn't aware of such selective declarations!!But a 180k veteran attested to it that that user is correct!!.So can you state this in a clearer,more detailed and more rigorous way?What exactly does `const int *ptr=&i; ` mean?

2)我也被告知我们不能横亘在声明以程序 const int的* PTR =放;我; 通过向pointer.What分配非恒定的地址是什么意思?为什么我们允许这样做吗?我们为什么不得到一个警告,如果我们指定一个非常到地址指针 PTR 这需要一个恒定的地址吗?如果是这样宽容有关被分配的非恒定的,为什么它抛出一个错误的地址,当我们试图改变的价值的,非恒定的,这是做一个合理的事,尖变量是一个非恒定

2) I was also told that we can lie to the program in the statement const int *ptr=&i; by assigning the address of a non-constant to the pointer.What does it mean?Why are we allowed to do that?Why don't we get a warning if we assign the address of a non-constant to the pointer ptr which expects address of a constant?And if it is so forgiving about being assigned address of non-constant,why it throws an error when we try to change the value of that non-constant,which is a reasonable thing to do,the pointed variable being a non-constant?

#include <stdio.h>

int main ()
{
 int i=8;
 const int *ptr=&i;
 *ptr=9;
 printf("%d",*ptr);
}

错误: 只读位置分配* PTR'|

推荐答案

定义 const int的* PTR =放;我; 实质上说:我不会修改 I PTR 。它不说, INT PTR 指的是常量,它说, PTR 应不用于修改它

The definition const int *ptr = &i; essentially says "I will not modify i through ptr." It does not say that the int that ptr points to is const, it says that ptr should not be used to modify it.

此转换是允许的,因为它是有道理的:因为 I 不是常量,我的允许以修改它,但我也可以选择不对其进行修改。没有规则被打破时,我选择不修改<​​code> I 。而且,如果我创建一个指向 I ,并说:我不打算使用该指针修改 I ,这是也没关系。

This conversion is allowed because it makes sense: Since i is not const, I am allowed to modify it, but I can also choose not to modify it. No rule is broken when I choose not to modify i. And, if I create a pointer to i and say "I am not going to use this pointer to modify i", that is fine too.

你会想这样做是为了让您可以将对象的地址传递给一个例程需要一个指向常量对象的原因。例如,考虑的strlen 程序。在的strlen 程序不会修改它的输入,所以它的参数是指向为const char 。现在,我有一个指针字符,我想知道它的长度。于是我打电话的strlen 。现在我一个指针传递给字符作为一个参数一个参数是指向为const char 。我们希望这样的转换工作。它使完整意义上的:修改我的字符如果我想,但的strlen 常规是不会,所以它会将它们视为为const char

The reason you would want to do this is so that you can pass the address of an object to a routine that takes a pointer to a const object. For example, consider the strlen routine. The strlen routine does not modify its input, so its parameter is a pointer to const char. Now, I have a pointer to char, and I want to know its length. So I call strlen. Now I am passing a pointer to char as an argument for a parameter that is pointer to const char. We want that conversion to work. It makes complete sense: I can modify my char if I want, but the strlen routine is not going to, so it treats them as const char.

1),你得到 * PTR =一些错误; ,因为你声明 PTR 为指针,以 const int的,但你侵犯了您的承诺不使用 PTR 修改 INT 。该 PTR 指向一个对象不是常量并不能否定你的承诺不使用<$ C $事实C> PTR 进行修改。

1) You get an error with *ptr = something; because you declared ptr as a pointer to const int, but then you violated your promise not to use ptr to modify the int. The fact that ptr points to an object that is not const does not negate your promise not to use ptr to modify it.

2)它不是一个谎言来分配非的地址 - 常量对象的指针常量。的分配不会说对象是常量,它说,指针不应该被用于修改的对象。

2) It is not a lie to assign the address of a non-const object to a pointer to const. The assignment does not say that the object is const, it says that the pointer should not be used to modify the object.

此外,虽然你有没有问过这个,在用C常量属性不完全绑定。如果你定义一个对象是常量,你不应该修改它。不过,也有其他的情况,其中一个指向常量传递给它转换成一个指向一个常规非 - 常量。这实际上是在语言的缺陷,没有能力挽留所有必要的信息来处理常量中我们可能preFER的方式。一个例子是和strchr 程序。它的声明是的char *和strchr(为const char * S,INT C)。参数取值为const char * ,因为和strchr 不改变它的输入。然而,和strchr 程序的指针从衍生小号(它指向的字符串中的字符之一)。因此,在内部,和strchr 已转换后的为const char * 的char *

Additionally, although you have not asked this, the const attribute in C is not completely binding. If you define an object to be const, you should not modify it. However, there are other situations in which a pointer to const is passed to a routine that converts it to a pointer to non-const. This is effectively a defect in the language, an inability to retain all the information necessary to handle const in the ways we might prefer. An example is the strchr routine. Its declaration is char *strchr(const char *s, int c). The parameter s is const char * because strchr does not change its input. However, the pointer that strchr routines is derived from s (it points to one of the characters in the string). So, internally, strchr has converted a const char * to char *.

这允许你写code传递一个的char * 和strchr ,并使用返回的指针修改字符串,它是罚款。但它意味着,编译器不能完全保护你从错误中如通过一个为const char * 和strchr 和使用返回指针试图修改字符串,这可能是一个错误。这是在C缺点。

This allows you to write code that passes a char * to strchr and uses the returned pointer to modify the string, which is fine. But it means the compiler cannot completely protect you from mistakes such as passing a const char * to strchr and using the returned pointer to try to modify the string, which may be an error. This is a shortcoming in C.

这篇关于究竟是什么&QUOT; const int的* PTR =安培; I&QUOT;是指非常数?为什么接受地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆