Jump to content
  • Advertisement
Sign in to follow this  
mckoo

PTR tough concept

This topic is 5170 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
Advertisement
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
so it's impossible to compare the address of 2 ptrs....
instead we could just compare the values of the ptrs? rite?

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
pls give me a clear concept of it if the ptrs they were mot pointing to a common object..thx in advance

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!