I have two void *
, namely void *a
and void *b
. Lets say a
is at 0x804ae0
and b
is at 0x804aec
. How do I compare the two? I want to check if address of a
is lower than address of b
.
Asked
Active
Viewed 1,890 times
-1

WedaPashi
- 3,561
- 26
- 42

graphics_master
- 59
- 7
-
7Have you *tried* to just compare them? – Some programmer dude Jun 03 '15 at 08:22
-
3"I want to check if address of a is lower than address of b" - careful in your wording. There is a *world* of difference between the address *of* a pointer and the address a pointer *holds*. – WhozCraig Jun 03 '15 at 08:25
-
2"*I want to check if address of a is lower than address of b*": Are you sure you mean this? `a` is itself a pointer and so is `b`. If you just want to compare the pointers, you should be saying "I want to check if `a` is lower than `b`". – ArjunShankar Jun 03 '15 at 08:26
-
My bad on that. But the compiler raised issues and said comparision by invalid cast. – graphics_master Jun 03 '15 at 08:49
-
@graphics_master Please provide a MVCE (http://stackoverflow.com/help/mcve) with the returned error then it can get fixed. – Persixty Jun 03 '15 at 09:42
5 Answers
11
The standard operators work on void *
too:
if (a < b) ...

Theodoros Chatzigiannakis
- 28,773
- 8
- 68
- 104

Erik Man
- 824
- 4
- 17
-
1Is it defined how these pointers are compared in this case? As signed or unsigned values? – dlask Jun 03 '15 at 08:27
-
1@dlask Signed/unsigned doesn't make sense for pointer addresses, so no. On the machine code level, the addresses are simply compared, and there is no such thing as "signed" existing there. – Lundin Jun 03 '15 at 08:31
-
@dlask pointers don't have a sign. They are addresses. According to the standard, they don't even have to be numbers (although in all hardware that I know of, they are). – ArjunShankar Jun 03 '15 at 08:32
-
OK, but only numbers can be compared, and since it's allowed to compare pointers there must be a way to map pointers to numbers. CPUs usually support signed and unsigned operations so the compiler designer has to decide which kind of arithmetic operation to use in this case. No offence, I am just curious. – dlask Jun 03 '15 at 08:34
-
see https://stackoverflow.com/questions/6702161/pointer-comparisons-in-c-are-they-signed-or-unsigned – Erik Man Jun 03 '15 at 08:35
-
1@dlask As far as C is concerned, 6.5.8: `"When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to."` and so on. As far as the machine code is concerned, there exists no such thing as pointers, just addresses and possibly index registers meant to be used to hold addresses. There exists no CPU with negative addresses, so why would anyone would use signed comparison for addresses? – Lundin Jun 03 '15 at 08:42
-
Comparison is valid iif the two pointers point to the same object (struct or array) or the logical location immediately after it. In either case `a – Persixty Jun 03 '15 at 08:45
-
1By 6.5.8/5, for data pointers this is viable so long as `a` and `b` fulfill the requirement that they address the same object, objects in an array, or hold the one-past address. If those conditions aren't met, this will invoke undefined behavior. – WhozCraig Jun 03 '15 at 08:45
-
1@DanAllen The specification is fully clear about typed pointers. The only problem here is with untyped pointers `void*`. – dlask Jun 03 '15 at 08:48
-
2@dlask A reasonable point. Maybe this needs tagging 'language lawyer'. The specification isn't crystal clear about `void*` (C99 - 6.5.8) and given how opaque it is that they can't be subtracted (6.5.6) it's fair to ask whether they can be (relationally) compared. I think the practical answer is (to the limit of my knowledge) implementers have generally taken the sentence 'When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to.' as applying to `void` pointers. – Persixty Jun 03 '15 at 09:10
-
2@DanAllen I like your comment very much, especially "*it's fair to ask whether they can be (relationally) compared*". – dlask Jun 03 '15 at 09:13
-
1@dlask However I don't think graphics_master is asking the question at the level formal compliance that you are. GM is asking 'how do I compare void*' to which the answer is 'easily'. We're discussing how that is formally defined by the language specification to which the answer is sort of and at best implicitly. – Persixty Jun 03 '15 at 09:44
1
You can compare directly .
int main(void)
{
int a[]={10,20};
void *p1=&a[0],*p2=&a[1];
if(p1>p2)
printf("Crazy\n");
else
printf("Correct\n");
return 0;
}
Expected output:
Correct

Persixty
- 8,165
- 2
- 13
- 35

Venkata Naidu M
- 351
- 1
- 6
-
I've modified this answer. It was undefined behavior. `a` and `b` don't point to the same object or two objects in the same array. – Persixty Jun 03 '15 at 08:50
-1
#include <stdint.h>
if ( (uintptr_t)a < (uintptr_t)b ) ...
Please note that typecast to int
could be insufficient on systems with 32bit integers but 64bit pointers.

dlask
- 8,776
- 1
- 26
- 30
-3
Every address is ultimately an number, a very long one but a number, so by doing a cast to an int(on a 64 bit system) or int64 you can compare the 2 values
if((int64)a < (int64)b)
{
.....
}

Tandura
- 866
- 2
- 7
- 19
-
1You don't need to convert them to integers. And if you for some reason need to do that, you should be using uintptr_t anyhow. – Lundin Jun 03 '15 at 08:34
-
There is no need to perform this cast and it could confound the 'right' semantics because it will perform a signed comparison. You could (in principle) have an array in an unsigned address space (usual) where a precedes b in an array but a – Persixty Jun 03 '15 at 08:48