PTR tough concept
excuse me...
is that ptrs cannot make any comparisions when they do not point to a common object?
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
char s[80], y[80];
char *p1, *p2;
p1 = s;
p2 = y;
if(p1<p2).............
any replies are appreciated
Well for what you put there, the function strcmp or stricmp will do what you need. It compares two strings and returns 0 if they are the same.
As for the general question comparing two pointers like that compares the addresses they point to. To compare what they point to use the indirect operator *, ie:
As I said before, use those functions for string comparison. If you want to compare two objects then make sure they have the correct operator overloaded, ie
Where ClassName is the object type and Value is the member that should decide if one object is 'less' than the other.
EDIT:
Sorry should've stated the indirect operator more clearly. Using * before a pointer accesses the value at the pointer, otherwise you are always using the address the pointer holds instead.
As for the general question comparing two pointers like that compares the addresses they point to. To compare what they point to use the indirect operator *, ie:
int i = 0;int j = 1;int* ptr1 = &i;int* ptr2 = &j;if(*ptr1 < *ptr2)std::cout << "i\n";else if(*ptr2 < *ptr1)std::cout << "j\n";
As I said before, use those functions for string comparison. If you want to compare two objects then make sure they have the correct operator overloaded, ie
bool operator < (const ClassName rhs) {if(Value < rhs.Value)return(true);else return(false); }
Where ClassName is the object type and Value is the member that should decide if one object is 'less' than the other.
EDIT:
Sorry should've stated the indirect operator more clearly. Using * before a pointer accesses the value at the pointer, otherwise you are always using the address the pointer holds instead.
so it's impossible to compare the address of 2 ptrs....
instead we could just compare the values of the ptrs? rite?
instead we could just compare the values of the ptrs? rite?
Quote:Original post by mckoo
so it's impossible to compare the address of 2 ptrs....
instead we could just compare the values of the ptrs? rite?
You can compare the addresses of the 2 pointers, but it won't be of all that much use, unless you're comparing (p1==p2) (or, even easier, s==y) to see if they are pointing to the same memory location (or not).
pls give me a clear concept of it if the ptrs they were mot pointing to a common object..thx in advance
Pointers are just memory addresses. On a modern pc that is usually 32bit (unless you are running a 64 bit machine + OS). So a pointer is basically an unsogned long integer, so yes you can compare any point address to any pointer address.
However as DJHoy said, comparing two pointers is usually meaningless. All they tell you is location in memory. Theres not that many situations when you need to know if an object is before another object in memory.
It might be easier for us to answer your question if you explain what you are trying to do by comparing two addresses of different objects.
However as DJHoy said, comparing two pointers is usually meaningless. All they tell you is location in memory. Theres not that many situations when you need to know if an object is before another object in memory.
It might be easier for us to answer your question if you explain what you are trying to do by comparing two addresses of different objects.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement