- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
考虑以下 2 个程序 prog1 和 prog2。这里如果我尝试更改 const
限定变量的值 i
使用指针 ptr
,我收到警告(不是错误)“初始化从指针目标类型中丢弃限定符|”
,但程序仍然运行并显示新值。但是如果我尝试使用赋值语句在第二个程序中更改 i
的值,我会得到错误(不是警告)读取赋值- 仅变量“i”|
。
以下是由此前提引起的混淆:
1)为什么我们在任何情况下都允许更改只读 const
限定变量的值?这是否违背了使用 const
的目的限定符?如果我们尝试这样做,我们不应该得到一个错误吗?
2) 即使由于某些奇怪的原因我们被允许更改常量的值,为什么要区分使用指针更改只读 const
限定变量的值(这是允许的,带有警告)并通过使用赋值操作(这是不允许的并且给我们一个错误)?
//prog1
#include <stdio.h>
int main ()
{
const int i=8;
int *ptr=&i;
*ptr=9;
printf("%d",*ptr); //Prints new value nevertheless
}
警告:初始化丢弃指针目标类型的限定符|
//prog2
#include <stdio.h>
int main()
{
const int i=8;
i=10;
printf("%d",i);
}
错误:只读变量“i”的赋值|
编辑 H2CO3
这里我不止一次改变了const
限定变量的值。我只得到一个警告,和prog1
//prog3
#include <stdio.h>
int main ()
{
const int i=8;
int *ptr=&i;
*ptr=9;
*ptr=10;
printf("%d",*ptr); //Prints 10
}
最佳答案
1) Why are we allowed to change the value of a read-only
const
qualified variable in any circumstance? Doesn't it defeat the purpose of using aconst
qualifier?
尝试通过赋值运算符更改 const 限定对象是违反约束的:
6.5.16 在约束下:
2 An assignment operator shall have a modifiable lvalue as its left operand.
可修改的左值在 6.3.2.1 (1) 中定义:
A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.
作为约束违规,它需要编译器根据 5.1.1.3 (1) 提供诊断消息:
A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.
但是不需要实现来拒绝无效程序,因此诊断消息也可以是警告而不是错误。
但是,通过没有 const 限定类型的左值修改声明为 const
的对象不是约束违规,尽管它会调用未定义的行为,6.7.3 (6):
If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined.
由于它既不是约束违规也不是无效语法,因此甚至不需要发出诊断消息。
Shouldn't we get an error if we attempt to do so?
如果您尝试通过具有 const 限定类型的左值修改对象,您必须得到诊断消息。
由于严重违反了声明的意图,大多数编译器在这些情况下都会发出错误。
如果您尝试通过具有非 const 限定类型的左值来修改具有 const 限定类型的对象,如下所示
const int i=8;
int *ptr=&i;
*ptr=9;
尝试通过表达式 *ptr = 9
修改 i
会调用未定义的行为,但这不是约束违规(或语法错误),因此不需要诊断信息(没有给出)。
初始化时会发出诊断消息
int *ptr = &i;
因为根据 6.5.16.1 (1),这又是一个约束违规:
One of the following shall hold:
- the left operand has atomic, qualified, or unqualified arithmetic type, and the right has arithmetic type;
- the left operand has an atomic, qualified, or unqualified version of a structure or union type compatible with the type of the right;
- the left operand has atomic, qualified, or unqualified pointer type, and (considering the type the left operand would have after lvalue conversion) 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;
- the left operand has atomic, qualified, or unqualified pointer type, and (considering the type the left operand would have after lvalue conversion) one operand is a pointer to an object type, and the other is a pointer to a qualified or unqualified version of void, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;
- the left operand is an atomic, qualified, or unqualified pointer, and the right is a null pointer constant; or
- the left operand has type atomic, qualified, or unqualified _Bool, and the right is a pointer.
然而,该诊断通常是警告而不是错误,因为人们可能会显式地放弃 const
,
int *ptr = (int*)&i;
而不能从 i
中丢弃 const
。
通过指向非 const 限定对象类型的指针修改对象是有效的如果指向的对象是可修改的。愚蠢的例子:
int i = 8;
const int *cptr = &i; // valid, no problem adding const
int *mptr = (int*)cptr;
*mptr = 9; // no problem, pointee is non-const
2) Even if due to some strange reason we are allowed to change values of constants, why the discrimination between changing the value of a read-only const qualified variable using a pointer (which is allowed,with a warning) and through using an assignment operation (which is simply not allowed and gives us an error)?
直接分配给具有 const 限定类型的对象不仅违反约束,而且明显违反规定的语义。声明一个对象 const
明确表示“我不想修改该对象”。
通过指向非 const 限定类型的指针修改对象不是约束违规,只有当指针具有 const 限定类型时才会出现未定义行为。允许将指向 const 限定类型的指针转换为指向对应的非 const 限定类型的指针,并且通过该指针修改指针对象可能是有效的,因此您只会收到警告,并且仅当未进行转换时明确的。
在给定的简短示例中,编译器可以检测到指针对象具有 const 限定类型,因此修改会调用未定义的行为,但通常很难检测到,而且通常无法检测到。因此,编译器甚至不尝试检测简单情况,不值得付出努力。
关于c - 为什么允许我们更改 "const"限定变量的值?为什么允许指针而不是赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16542817/
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: How to nest OR statements in JavaScript? 有没有办法做到这一点:
在 JavaScript 中有没有办法让一个变量总是等于一个变量?喜欢var1 = var2但是当var2更新,也是var1 . 例子 var var1 = document.getElementBy
我正在努力理解这代表什么 var1 = var2 == var3 我的猜测是这等同于: if (var2 == var3): var1 = var2 最佳答案 赋值 var1 = var2
这个问题已经有答案了: What does the PHP error message "Notice: Use of undefined constant" mean? (2 个回答) 已关闭 8
我在临时表中有几条记录,我想从每条记录中获取一个值并将其添加到一个变量中,例如 color | caption -------------------------------- re
如何将字符串转为变量(字符串变量--> $variable)? 或者用逗号分隔的变量列表然后转换为实际变量。 我有 2 个文件: 列名文件 行文件 我需要根据字符串匹配行文件中的整行,并根据列名文件命
我有一个我无法解决的基本 php 问题,我也想了解为什么! $upperValueCB = 10; $passNodeMatrixSource = 'CB'; $topValue= '$uppe
这可能吗? php $variable = $variable1 || $variable2? 如果 $variable1 为空则使用 $variable2 是否存在类似的东西? 最佳答案 PHP 5
在 Perl 5.20 中,for 循环似乎能够修改模块作用域的变量,但不能修改父作用域中的词法变量。 #!/usr/bin/env perl use strict; use warnings; ou
为什么这不起作用: var variable; variable = variable.concat(variable2); $('#lunk').append(variable) 我无法弄清楚这一点
根据我的理解,在32位机器上,指针的sizeof是32位(4字节),而在64位机器上,它是8字节。无论它们指向什么数据类型,它们都有固定的大小。我的计算机在 64 位上运行,但是当我打印包含 * 的大
例如: int a = 10; a += 1.5; 这运行得很完美,但是 a = a+1.5; 此作业表示类型不匹配:无法从 double 转换为 int。所以我的问题是:+= 运算符 和= 运算符
您好,我写了这个 MySQL 存储过程,但我一直收到这个语法错误 #1064 - You have an error in your SQL syntax; check the manual that
我试图在我的场景中显示特定的奖牌,这取决于你的高分是基于关卡的目标。 // Get Medal Colour if levelHighscore goalScore { sc
我必须维护相当古老的 Visual C++ 源代码的大型代码库。我发现代码如下: bIsOk = !!m_ptr->isOpen(some Parameters) bIsOk的数据类型是bool,is
我有一个从 MySQL 数据库中提取的动态产品列表。在 list 上有一个立即联系 按钮,我正在使用一个 jquery Modal 脚本,它会弹出一个表单。 我的问题是尝试将产品信息变量传递给该弹出窗
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the difference between (type)value and type(va
jQuery Core Style Guidelines建议两种不同的方法来检查变量是否已定义。 全局变量:typeof variable === "undefined" 局部变量:variable
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: “Variable” Variables in Javascript? 我想肯定有一种方法可以在 JavaScrip
在语句中使用多重赋值有什么优点或缺点吗?在简单的例子中 var1 = var2 = true; 赋值是从右到左的(我相信 C# 中的所有赋值都是如此,而且可能是 Java,尽管我没有检查后者)。但是,
我是一名优秀的程序员,十分优秀!