Kim Kardashian
Kim Kardashian

Reputation: 91

How are pointers compared?

int palindrome(char * str){
char * pt1 = str;
char * pt2 = str;
    if(pt1==NULL){
        std::cout<<"\n invalid string";
        return -1;
    }
    while(*pt2 != '\0')
        pt2++;
    pt2--;
    while(pt1 < pt2){ // why does this work correctly ?
        if(*pt1 != *pt2){
            std::cout<<"\n not palindrome";
            return 0;
        }
        else{
        pt1++;
        pt2--;
        }
    }
std::cout<<"\n yes palindrome";
return 1;
}

hello
this is a function to check if the passed char* points to a palindrome/not.
here two pointers
pt1 - starts from begining moves fwds
pt2 - starts from end moves backwards
now i do not want them to continue once they meet in the middle..
SO i check if always pt1 why? i am not comparing *pt1 vs *pt2.
what values does it compare ?

Upvotes: 2

Views: 181

Answers (5)

Luke Walsh
Luke Walsh

Reputation: 281

These are the three concepts leading this block of code executing correctly:

  1. Your pointers are operating on a continuous block of memory (the input string)
  2. The pointer 'values' are addresses (basically just arbitrary numbers allocated at runtime)
  3. C allocates addresses for continuous memory in a well defined, increasing manner

Due to the fact that pointers are really just integers (such as 0x000001, 0x000002 etc) you can use comparison operators on them.

Lastly due to the fact that the memory for the string will be continuous and increasing, there is a level of abstraction that you can make: Pointers further along in memory will evaluate as greater than addresses earlier in memory.

Upvotes: 3

s.bandara
s.bandara

Reputation: 5664

There two kinds of comparison you use in your code, pt1 < pt2 and *pt1 != *pt2. In the first case, you compare memory addresses, i.e. where in the string you are right now. Once pt1 >= pt2, your pointers are crossing or will have crossed. In the second case, you dereference pointers using the *-operator and compare the values these pointers point to.

Upvotes: 1

chris
chris

Reputation: 412

Pointers are addresses. When you compare two pointers it's comparing their addresses, which is wrong for your use. You'll need to maintain an offset or other variables of the integer kind to figure out when the positions meet.

Upvotes: -3

Billy ONeal
Billy ONeal

Reputation: 106609

Pointers model memory addresses. Because the first pointer points somewhere in memory before the second, the less than comparison succeeds.

They don't continue when they meet in the middle because when a == b, then a < b must be false.

Upvotes: 1

Patashu
Patashu

Reputation: 21793

A pointer points to a place in memory. The value of a pointer is thus a memory address. For a given memory allocation, every byte in it is contiguous and has the next higher address assigned to it (0x0000, 0x0001, 0x0002 and so on) So, when a pointer is greater than another pointer and both pointers belong to the same memory allocation, it is further along in said allocation.

Upvotes: 4

Related Questions