const在这种情况下的作用是 – const int *ptr;

huangapple go评论61阅读模式
英文:

What does const do in this case - const int *ptr;

问题

我在这里进行了测试,无法确定它是否有任何作用... 我可以更改它所指向的地址以及该地址上的值。

#include <stdio.h>;

int main() {
  // p_int是指向int的指针,而该int是常数
  const int *p_int;
  int x = 5;
  int y = 6;
  p_int = &x;
  p_int = &y;
  y = 3;
  return 0;
}

我不确定如何进一步测试。

英文:

I tested it here and can't tell that it does anything ... I can change both the address it points to and the value at at that address.

#include &lt;stdio.h&gt;

int main() {
  // p_int is a pointer to an int, and that int is constant
  const int *p_int;
  int x = 5;
  int y = 6;
  p_int = &amp;x;
  p_int = &amp;y;
  y = 3;
  return 0;
}

I'm not sure how to test further.

答案1

得分: 4

variable p_int 是指向常量整数的指针。

你不能使用该指针更改整数的值。

因此,类似 *p_int = 3; 的语句将失败。

当我尝试时,我会得到以下错误:

error: assignment of read-only location ‘*p_int’

你可以更改变量 y,因为它没有声明为 const

#include <stdio.h>

int main() {
  // p_int 是指向整数的指针,而该整数是常量
  const int *p_int;  // 指向常量整数的指针:
                     // 注意:可以重新分配指针。
                     // 但不能分配指针引用的值。
  int x = 5;         
  int y = 6;
  p_int = &x;
  p_int = &y;
  y = 3;
  *p_int = 3;        // 错误:无法分配只读位置 '*p_int'
  return 0;
}
英文:

variable p_int is a pointer to a Constant Integer.

You cannot change the value of the integer using that pointer.<br>

So a statement like *p_int = 3; will fail.

When I try, I get:

error: assignment of read-only location ‘*p_int’

You are free to change variable y, because it was NOT declared const.

#include &lt;stdio.h&gt;

int main() {
  // p_int is a pointer to an int, and that int is constant
  const int *p_int;  // Pointer-to-Constant Integer: 
                     // NOTE: the pointer can be re-assigned.
                     // But the value referenced by the pointer cannot be assigned.
  int x = 5;         
  int y = 6;
  p_int = &amp;x;
  p_int = &amp;y;
  y = 3;
  *p_int = 3;        // error: assignment of read-only location ‘*p_int’
  return 0;
}

答案2

得分: 0

const in this case requires the compiler to warn about certain misuses of the pointer, such as attempting to assign to a non-modifiable lvalue or discarding qualifiers in an assignment or initialization. When you define an object with const, it allows the compiler to protect the object from inadvertent modification and make optimizations based on the assertion that the object won't be modified by the program. However, your declaration does not show such a use of const.

英文:

> [What] does const do in this case - const int *ptr;

Primarily what const in that declaration does is require the compiler to warn you about certain misuses of the pointer. It does that by triggering these constraints in C 2018 6.5.16 1 and 6.5.16.1 1:

> [6.5.16 1] An assignment operator shall have a modifiable lvalue as its left operand.

> [6.5.16.1 1] … both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;…

and then the consequence of violating a constraint is that the compiler must produce a diagnostic message, per C 2018 5.1.1.3 1:

> A conforming implementation shall produce at least one diagnostic message … if … a translation unit contains a violation of any syntax rule or constraint…

That is the primary effect: The compiler must help the programmer avoid mistakes by diagnosing these misuses of things qualified with const.

> I'm not sure how to test further.

You can test this by inserting either of these lines and attempting to compile:

*p_int = 0; // Will warn of a violation of the first constraint, attempting to assign to a non-modifiable lvalue.

int *q = p_int; // Will warn of a violation of the second constraint, discarding qualifiers in an assignment or initialization.

(The constraints apply directly to assignments, but other parts of the C standard, such as the rules for initialization, some conversions, and compound assignments, refer to the rules for assignments or simple assignments.)

When you define an object with const (not just const on a pointer to an object, but const on the original definition of an object), then the compiler is allowed to put the object in memory protected from inadvertent modification and to make optimizations based on the assertion that the object will not be modified by the program. However, your declaration does not show such a use of const.

huangapple
  • 本文由 发表于 2023年3月21日 03:17:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794428-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定