what is ISO C++ forbids comparison between pointer and integer?

Recommended Answers

All 9 Replies

Because they are two different things.
I suspect you are referring to an error you're getting?
Post the code (using [code] [/code] tags) and we might come up with a solution

Because of no total linear memory space concept in platform-independent C++ language.
Moreover, no assumption that pointer binary representations are positive (or negative or even if they are valid) integer values. For example, visual pointer representation on some platforms looks like XXXX:YYYY - is it integer?

You can use reinterpret_cast<> but the effect is undefined by the language standard...
A pointer arithmetics is the other story. Its definition is a very cautious and accurate so does not violate the principles mentioned above...

Because of no total linear memory space concept in platform-independent C++ language.

That's not true. The memory model in C and C++ is linear. Or, at least, the memory available to a program consists of a series of one or more sequences of contiguous bytes.

However, a pointer is not the same thing as an integer, so comparison between them is not allowed.

To grumpier:
full ordering and partial ordering - feel the difference. Look at the TOTAL word in my post before make the conclusion "That's not true".
The last sentence in you post does explain nothing: "a pointer is not the same thing as an integer, so comparison between them is not allowed" but godlike111 asked why?..

>but godlike111 asked why?
Actually, the OP asked "what". ;) But the "why" is because that's a specific constraint in the standard. The fundamental "why" is because pointers are maps to a location, not the location itself. Optimization concerns aside, there's no reason why a pointer couldn't be represented by an array or a structure with all kinds of useful information as long as the required conversions are supported. Such comparisons are incompatible by nature.

> what is ISO C++ forbids comparison between pointer and integer?
Or ISO C++ forbids comparison between elephant and battle ship.

The fact that both are large, grey and heavy doesn't mean that they are in any way the same thing.

In the same way that pointers and integers are represented by something similar (ie, bits), doesn't make the whole entities comparable.

Whilst you can compare any integer with any other integer, the same is not true of pointers. Pointer comparison is only allowed where the two pointers point to members of the same object (say for example, two elements of an array). Picking two pointers at random (say the result of two malloc calls) and trying to compare them is undefined.

To grumpier:
full ordering and partial ordering - feel the difference. Look at the TOTAL word in my post before make the conclusion "That's not true".

My original comment stands. The word "total" does not make your post any more correct.

The last sentence in you post does explain nothing: "a pointer is not the same thing as an integer, so comparison between them is not allowed" but godlike111 asked why?..

You obviously can't understand simplified language. The "why" is that pointers and integers represent completely unrelated things (concepts, quantities, abstractions....), so there is no sense in comparing them.

My original comment stands. The word "total" does not make your post any more correct.
You obviously can't understand simplified language. The "why" is that pointers and integers represent completely unrelated things (concepts, quantities, abstractions....), so there is no sense in comparing them.

It seems you are trying to explain truismes. The char and int values are different things too but we can compare these values in C++ w/o any casting. Moreover, we can compare bool and int (bool is not a number!).

On the other hand all beginners (and veterans too) can see hex integer-like pointer representations in IDE and on the screen after cout << pointer. They can add an integer to a pointer, subtract an integer from a pointer. So the true question was (it's safe to say): they are very much alike, why this bad C++ Standard forbid to compare them?

In that case the answer "because they are different things" is a common truth only...

>The char and int values are different things too but we can
>compare these values in C++ w/o any casting. Moreover,
>we can compare bool and int (bool is not a number!).
That's largely inaccurate. bool, char, and int are all part of the family of integer types. While the range of values will vary, they're all ultimately integral values. Supporting comparison between them is logical. And yes, bool is indeed a number.

>On the other hand all beginners (and veterans too) can see hex integer-like
>pointer representations in IDE and on the screen after cout << pointer.
You can print an integer as hexadecimal, octal, decimal, binary, or whatever other representation you choose to support. Which one should be used for comparisons? The situation you describe is simply a fundamental misunderstanding of the difference between the display representation for your compiler and the storage representation.

>They can add an integer to a pointer, subtract an integer from a pointer.
Which is neither a comparison nor integer arithmetic. It's a process that shifts a pointer by an offset.

>So the true question was (it's safe to say): they are very much
>alike, why this bad C++ Standard forbid to compare them?
It's a question born of misunderstanding, but it is a viable question. The answer, of course, is that allowing it introduces a lot of extra work for implementations when the feature has questionable benefit. It clearly affects portability of the language, which might require many changes elsewhere in the standard to maintain the current level of portability. It affects correctness of programs, which would likely introduce restrictions elsewhere to avoid unexpected behavior.

The C++ standard forbids it because that's probably the best option. It may not be ideal, but the alternatives would likely be more restrictive and then you'd be asking why the standard forbids other things that are prohibited to support comparisons of pointers and integers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.