2

C99 6.3.2.3/3 An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.55) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

Does it say that two null pointers don't compare equal? But they do:

int *a = 0;
int *b = 0;

assert(a == b); // true

I wonder what is unequal in this case?

Jens
  • 69,818
  • 15
  • 125
  • 179
user963241
  • 6,758
  • 19
  • 65
  • 93

2 Answers2

5

You need to read the Standard carefully: A null-pointer compares unequal to any pointer to an object or function. A null-pointer cannot be generated by using the address-of operator on an object (e.g. &foo). Any function pointer is guaranteed to be non-NULL. Taken together these two imply that a null-pointer never compares equal to a ptr-to-object or ptr-to-function.

To clarify, to compare unequal means that a == b is false (identical with a != b is true).

The paragraph you cited does not say anything about comparing two null-pointers, but the next paragraph does:

Conversion of a null pointer to another pointer type yields a null pointer of that type. Any two null pointers shall compare equal.

To compare equal means that a == b is true (identical with a != b is false).

Jens
  • 69,818
  • 15
  • 125
  • 179
  • 1
    +1 for strong clarification. The glaring generality of the last sentence (of the cited paragraph) is especially meaningful, as it concludes that NULL pointers of *any* type will compare equally. This is more important than it first seem. Nice summary, btw. – WhozCraig Dec 03 '12 at 18:11
0

Please correct me if am wrong in interpreting the statement.

int main() {
   void* a = (void *)0;
   int* b;    // pointer b is of different type to a
   printf((a == b) ? "equal" : "not equal");
   return 0;
}

Result:

not equal

The statement,

If a null pointer constant is converted to a pointer type, the resulting pointer, called a null
pointer, is guaranteed to compare unequal to a pointer to any object or function.

says, the two pointers can be compared equal if and only if both are null pointers or pointers of same object or function and unequal when you compare two different pointer types.

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
  • 1
    This proves nothing since `b` is used uninitialized, leading to undefined behavior. – Jens Dec 04 '12 at 09:13
  • @Jens The idea here is pointers compared unequal when they are different types by interpreting statement. Yes, agreeing with you pointer b should be initialized. – Sunil Bojanapally Dec 04 '12 at 09:22
  • That does not make sense. Pointers to different types can compare equal, even if non-null. Think of ptr-to-void compared with a ptr-to-anything, where the former was assigned from the latter. – Jens Dec 04 '12 at 09:28
  • @Jens Yes it is compared equal when "where the former was assigned from the latter". Meaning only when pointers (a!=b) is false compared unequal. – Sunil Bojanapally Dec 04 '12 at 09:46
  • SunEric, I really don't know what you are trying to tell me, likely because both of us are not native speakers. However, your main() function is bogus, it is undefined behavior and it does not support whatever it is you are trying to tell us. Would you consider rewriting it with assignment to `b` and then rerunning it? – Jens Dec 04 '12 at 10:05