Sign in to follow this  

PTR tough concept

This topic is 4865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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:


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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this