0

Can I compare two pointers in case that some pointer variable is greater than another? Below is a simple example:

#include <stdio.h>


int main()
{
    int age;
    int allowedAge = 18;
    int* pointerAge = &age;
    int* pointerAllowed = &allowedAge;
    
    printf("How old are you?\n");
    scanf("%d", pointerAge);
    
        if(pointerAge < pointerAllowed)
        printf("You are only %d so you cannot drink any alcohol\n", *pointerAge);
        else
        printf("You are %d so you can drink\n", *pointerAge);
        
    return 0;
}
  • 1
    The program looks like you wanted to compare the values the pointers point to, instead of the pointers themselves. Can you clarify? – harold Mar 25 '23 at 10:36
  • You do not want to compare pointer values. What you want is to compare the values where the pointers point to. Big difference. Compare `*pointerAge < *pointerAllowed` – Gerhardh Mar 25 '23 at 10:37
  • If you compare pointers, you compare the address values stored in them. As your `printf` shows, you are already aware that the desired value is in `*pointerAge`, not `pointerAge`. – Gerhardh Mar 25 '23 at 10:39
  • If the pointers ptr1, ptr2 are pointing to parts of the same array, then if ptr1 is pointing to an element with a greater index then ptr1 - ptr2 > 0. I think the OP wants to ask if its valid c to write ptr1 > ptr2 instead. – Simon Goater Mar 25 '23 at 11:15
  • 1
    This question seems to be based on a misunderstanding, so I will vote for closing as such. The question in the headline is already answered here: https://stackoverflow.com/questions/11713929/c-c-pointer-arithmetic – nielsen Mar 25 '23 at 12:43
  • Besides the other comments you've gotten, I will urge you to, at some point, learn what pointers are really good for. They're fantastically useful, but it's hard to explain what they are at first, so instructors often use wildly unrealistic examples, such as this one. If all you want to do is compare one age to one threshold, there's no reason to use pointers; they only get in the way and confuse things. – Steve Summit Mar 29 '23 at 14:31
  • Also, whenever you use pointers, it's vitally important to maintain a clear distinction in your mind between *the pointer* versus *what the pointer points to*. It's easy, but always quite wrong, to mix those two things up. In this case, what you want to compare is the pointed-to values (the age and the allowed age), but what you're actually trying to compare with `if(pointerAge < pointerAllowed)` is the pointers themselves. – Steve Summit Mar 29 '23 at 14:33
  • It's like hearing that your girlfriend sent you a letter (an actual, paper letter), and running out and desperately trying to read your mailbox, when what you actually want to read is the letter *inside* the mailbox. – Steve Summit Mar 29 '23 at 14:34

2 Answers2

2

Can I compare two pointers in case that some pointer variable is greater than another?

No. Comparing pointers for great-ness is undefined behavior (UB) unless those pointers are within the same object such as pointers to elements within an array (or one past).


Yet given OP's printf(), the coding goal is not to compare pointers but to compare the objects they point to.

    // if(pointerAge < pointerAllowed)
    if (*pointerAge < *pointerAllowed)
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

If you compare the pointers like this if(pointerAge < pointerAllowed), you are asking if the memory address where pointerAge is stored is less than the memory address where pointerAllowed is stored.

However, if you dereference the pointers like this if(*pointerAge < *pointerAllowed), you will be asking if the value contained in pointerAge is less than the value contained in pointerAllowed.

izzy
  • 769
  • 2
  • 12
  • 22