Thread: what the comparison does?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    what the comparison does?

    Code:
    void func(int * p) { 
    
    printf("%s\n", p < (int*)(&p) ? "GREAT" : "LESS"); 
    }
    how the address are compared?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dpp
    how the address are compared?
    As pointers to int, I suppose. In what context did you see this code, or did you just come up with it yourself?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by pelicanpie View Post
    &p is the address of the variable p which happens to the store the address of where the int is stored
    Which in this case is the address of the local variable created for this function, to which p is assigned inside this function call:
    Code:
    #include<stdio.h>
    void func( int *p )
    {
        printf( "%p, %p\n", (void*) p, (void*) &p );
    }
    int main( void )
    {
        int x;
        int *p = &x;
        printf( "%p, %p\n", (void*) p, (void*) &p );
        func( p );
        
        return 0;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. std::string comparison versus int comparison
    By leeor_net in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2009, 07:28 AM
  3. Bug in iterator comparison in C++ standard?
    By steev in forum C++ Programming
    Replies: 14
    Last Post: 07-12-2008, 12:02 AM
  4. String Comparison
    By Cdrwolfe in forum C++ Programming
    Replies: 8
    Last Post: 03-27-2006, 10:59 AM
  5. comparison between pointer and integer
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-07-2006, 01:15 PM