Does equality compare two pointers that are not in the same array defined by behavior?

Consider please

int main(void)
{
    int a;
    int b;
    int* pa = &a;
    int* pb = &b;

    int e = pa == pb;
}

Is this clearly defined? paand pbdo not point to elements in the same array.

+4
source share
2 answers

If I can quote c11 Standard: §6.5.9¶2 Restrictions on ==:

One of the following:

  • both operands are of arithmetic type;
  • both operands are pointers to qualified or unskilled versions of compatible types;
  • one operand is a pointer to the type of object, and the other is a pointer to a qualified or unskilled version of void; or
  • , - .

, , - . (? §6.5.9¶6

, , ( ) , , - , - ,

1) (§6.5.9¶6) - , , , ==, . , , .   , , .


- . §6.5.8¶5 ( )

, ... undefined.

- - . , undefined.


:

, - - . - , , . ? . , , , . ? , . -

:

  • ;
  • .

- , ptr < NULL . , - §6.5.8¶5. , , ptr < NULL , , .

+7

.

6.5.9 C (==, !=) :

2 :

  • ;
  • ;
  • , - void
  • , - .

...

5 . , - , . , - void, .

6 , , ( ) , , , - , .

2 , , , void *, NULL. 6 , , .

6.5.8, (<, >, <=, >=), , :

5 , . , . , , , , , , , , .
. P , Q , Q + 1 , P, undefined

, , , undefined . 6.5.9 .

, :

char a[5], b[5];

6.5.9p6 a + 5 == b true, a b.

+7

Source: https://habr.com/ru/post/1694543/


All Articles