指针和整数C中的比较和赋值 [英] Comparison and assignment between pointer and integer C

查看:191
本文介绍了指针和整数C中的比较和赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于这两个报告的理论问题:

I've a theoretical question about these two statements:

假设 P 一个指向整数和 A 的整数:

Assuming p a pointer to an Integer and a an Integer:

A)如果(P ==一){......} 如果(P>将) ..

B) P = A ;

他们都是非法和 B 是特别危险的,但如何标准C考虑呢?

All of them are illegal and b is especially dangerous but how does standard C consider them?

阅读的标准,我没有发现,如果他们是错误的,不确定的行为,未指定的行为,违反约束,如果其中一人是法人或其他。

Reading the standard, I haven't found if they are errors, undefined behaviors, unspecified behavior, constraint violation, if one of them is legal or other.

展望无数类似的问题,我还没有找到一个解决方案。

Looking in the countless similar question, I haven't found a solution.

推荐答案

C11(n1570)§6.5.8[关系运算符/ 2:

C11 (n1570) §6.5.8 [Relational operators] / 2:

以下情况之一应当持有:

Constraints

One of the following shall hold:


      
  • 两个操作数具有的实型的;或

  •   
  • 两个操作数的指针的兼容对象的合格的或不合格的版本
      类型。

  •   
  • both operands have real type; or
  • both operands are pointers to qualified or unqualified versions of compatible object types.

(真正的类型意味着整数类型,枚举,真正的浮点类型和字符(§6.2.5/ 17))

(real type means the integer types, enums, real floating point types and char (§6.2.5/17))

§6.5.9[等式操作符]

§6.5.9 [Equality operators]

以下情况之一应当持有:

Constraints

One of the following shall hold:


      
  • 两个操作数具有的算术类型的;

  •   
  • 两个操作数的指针的兼容类型的合格的或不合格的版本;

  •   
  • 一个操作数是的指针的一个对象类型,另一个是的指针的到的合格或不合格的版本无效;或

  •   
  • 一个操作数是的指针的,另一个是一个的的指针常量。

  •   
  • both operands have arithmetic type;
  • both operands are pointers to qualified or unqualified versions of compatible types;
  • one operand is a pointer to an object type and the other is a pointer to a qualified or unqualified version of void; or
  • one operand is a pointer and the other is a null pointer constant.

§6.5.16.1[简单分配] / 1:

§6.5.16.1 [Simple assignment] / 1:

以下情况之一应当持有:

Constraints

One of the following shall hold:


      
  • 左操作具有原子,合格,不合格或算术类型的,并且右击具有的算术类型的;

  •   
  • 左操作数的原子,合格,不合格或版本的结构的或联合类型与右侧的类型兼容;

  •   
  • 左操作具有原子,合格,不合格或指针类型的和(考虑
      类型左操作数将有左值转换后)两个操作数都
      的指针的合格的或不合格的版本兼容的类型,类型尖头
      由左侧有类型的所有限定符由右侧指向;

  •   
  • 左操作具有原子,合格,不合格或指针类型的和(考虑
      左值转换后的左操作数将有)的类型一个操作数是一个指针
      一个对象类型,另一个是的指针的到的资格或不合格的版本
      无效,类型指向左边有类型的所有预选赛指出,
      通过权;

  •   
  • 左操作数是一个原子,合格,不合格或指针的,而右边是一个的
      指针常量;要么
      左侧操作数的类型的原子,合格,不合格或 _Bool ,而右边是一个
      的指针

  •   
  • the left operand has atomic, qualified, or unqualified arithmetic type, and the righ 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.

基本上,所有3个前pressions 违反限制在EX pression设置。

Basically, all 3 expressions violated the constraints set in the expression.

什么是违反约束是什么意思?错误?但无处说,违反约束须安排翻译失败。 C标准似乎不很清楚,但最近的跨pretation将是:

What does violating constraints mean? Errors? But nowhere says that violation of constraints shall cause the translation to fail. The C standard doesn't seem to be very clear, but the closest interpretation would be:

§4[一致性] / 2:

§4 [Conformance] / 2:

如果一个应该或不得的约束或运行约束之外出现违反规定,该行为是不确定的。

If a "shall" or "shall not" requirement that appears outside of a constraint or runtime-constraint is violated, the behavior is undefined.

但谈论的之外的约束;和§5.1.1.3[诊断] / 1:

but this talks about outside of constraints; and §5.1.1.3 [Diagnostics] / 1:

一个符合标准的实现必须产生至少一种诊断消息(在确定
   实现定义的方式),如果preprocessing翻译单位和翻译单位
    包含的违反任何语法规则或约束的,即使该行为也明确指定为未定义或实现定义。诊断信息不需要在其他情况下进行生产。

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.

所以编译器需要至少生成消息(正如我们可以看到GCC),但并没有说以后他们应该做的。

so the compiler needs to at least produce a message (as we can see in gcc), but did not say what they should do after that.

这个段落之后的例子提到,

The example after this paragraph mentions that

这是实施出具的诊断为翻译单元:

EXAMPLE

An implementation shall issue a diagnostic for the translation unit:

   char i;
   int i;

由于在措辞本国际标准描述了结构的行为既是约束错误和导致不确定的行为这种情况下的,约束误差应明确诊断。

because in those cases where wording in this International Standard describes the behavior for a construct as being both a constraint error and resulting in undefined behavior, the constraint error shall be diagnosed.

和§6.7[声明] / 3没有明确提到违反约束将导致不确定的行为。

and §6.7 [Declarations] / 3 does not explicitly mention that violating the Constraint will cause undefined behavior.

因此​​,我推断你的前3 pressions都是未定义行为,并伴有诊断信息

Therefore, I deduce that your 3 expressions are all undefined behaviors, accompanied with diagnostic messages.

这篇关于指针和整数C中的比较和赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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