How can I generally compare pointers? is it like comparing numbers??
How can I generally compare pointers? is it like comparing numbers??
You can compare to see if they point at the same location.
Or you could compare to see if the value at whatever location they point at, equals.Code:int* p1, p2; if ( p1 == p2 )
Code:int* p1, p2; if ( *p1 == *p2 )
This parameter is reserved
If the pointers point to the elements of the same array (or one past the end thereof) or aggregate (typically a struct object), then you can also use the relational operators (<, <=, >, >=) to compare for their relative locations.
By the way, Ktulu's post #2 has a common typo error with respect to declaring pointers. This:
declares p1 to be a pointer to int and p2 to be an int, whereas this:Code:int* p1, p2;
declares both p1 and p2 to be of type int.Code:int *p1, *p2;
To avoid this kind of mistake, I suggest that you declare them separately:
or equivalently:Code:int *p1; int *p2;
Code:int* p1; int* p2;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)