Sorry ! a typo in the code

Discussion in 'C Programming' started by Nitin Bhardwaj, Aug 2, 2004.

  1. Sorry I made a typo in the code
    The correct one is like this :

    #include <stdio.h>
    int main(void)
    {
    int *buf1 = malloc(10*sizeof(int));
    int *buf2 = malloc(10*sizeof(int));

    printf(buf1 > buf2 ? "Downwards" : "Upwards");
    return 0;
    }

    I'm posting thru Google thats why this posting is not in the same thread :-(
    Sorry for that !
     
    Nitin Bhardwaj, Aug 2, 2004
    #1
    1. Advertising

  2. Nitin Bhardwaj

    pete Guest

    Nitin Bhardwaj wrote:
    >
    > Sorry I made a typo in the code
    > The correct one is like this :
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > int *buf1 = malloc(10*sizeof(int));
    > int *buf2 = malloc(10*sizeof(int));
    >
    > printf(buf1 > buf2 ? "Downwards" : "Upwards");
    > return 0;
    > }


    That's undefined behavior.
    If you have (as you do here)
    two objects, which are not members of the same aggregate object,
    then the only comparisons which are allowed for their addresses,
    are that of equality and inequality.
    Greater Than and Less Than operations are not defined
    for the addresses of two separate objects.

    --
    pete
     
    pete, Aug 2, 2004
    #2
    1. Advertising

  3. (Nitin Bhardwaj) writes:
    [...]
    > I'm posting thru Google thats why this posting is not in the same thread :-(
    > Sorry for that !


    <OT>
    It's hard to believe that Google doesn't let you post a proper
    followup. (You might have to wait for your original article to
    show up first.)
    </OT>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Aug 2, 2004
    #3
  4. Nitin Bhardwaj

    Guest

    (Nitin Bhardwaj) wrote in message news:<>...
    > Sorry I made a typo in the code
    > The correct one is like this :
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > int *buf1 = malloc(10*sizeof(int));
    > int *buf2 = malloc(10*sizeof(int));
    >
    > printf(buf1 > buf2 ? "Downwards" : "Upwards");
    > return 0;
    > }


    Is this the way that we should compare two pointers?. Should we not use
    void* pointer for a compersion? buf1, buf2 are int* and "Downwards" : "Upwards"
    are string. The OP "forces" no conversion. Is he making a implicit conversion.
    Is that correct. I ask since no body pointed out this. Please clear my doubt.
     
    , Aug 4, 2004
    #4
  5. On Wed, 4 Aug 2004 wrote:
    >> int *buf1 = malloc(10*sizeof(int));
    >> int *buf2 = malloc(10*sizeof(int));
    >>
    >> printf(buf1 > buf2 ? "Downwards" : "Upwards");


    > Is this the way that we should compare two pointers?. Should we not use
    > void* pointer for a comparision?


    You can always compare two object pointers of like type which point to
    (parts of) the same object. For example, two pointers to 'int' can be
    compared with '>' if they both point into the same array. Or you can
    compare two pointers to 'char', or two void pointers. (There are other
    rules: for example, IIRC you can compare a void pointer to any other
    object pointer; but that's not nearly as important as the basic rule.)

    > buf1, buf2 are int* and "Downwards" : "Upwards" are string.


    Correct. Notice that the comparison of 'buf1' and 'buf2' is illegal,
    because while they have the right types, they don't point to comparable
    addresses --- they point to two different malloc'ed blocks of memory.

    > The OP "forces" no conversion. Is he making a implicit conversion.


    No. Both values are of type 'pointer to int' already; no conversions
    take place.

    The expression evaluates 'buf1 > buf2' (which is undefined, but let's
    pretend it returns 0 or 1 like a normal comparison), and then yields
    either "Downwards" (an array[10] of char) or "Upwards" (an array[8] of
    char). In either case, the array of char /decays/ to a pointer to char,
    which is then passed to 'printf'. 'printf' takes the string and prints
    it on the screen. No conversions happen except for the array-to-pointer
    decay, and that's nothing special.

    HTH,
    -Arthur
     
    Arthur J. O'Dwyer, Aug 4, 2004
    #5
  6. Nitin Bhardwaj

    Guest

    (Nitin Bhardwaj) wrote in message news:<>...
    > Sorry I made a typo in the code
    > The correct one is like this :
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > int *buf1 = malloc(10*sizeof(int));
    > int *buf2 = malloc(10*sizeof(int));
    >
    > printf(buf1 > buf2 ? "Downwards" : "Upwards");
    > return 0;
    > }


    OP is comparing the two pointers *buf1 and *buf1 but it has nothing
    to do with string printing in printf. Thanks Arthur J. O'Dwyer for
    pointing. I must be more carfull in reading.
     
    , Aug 4, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Fagen
    Replies:
    0
    Views:
    411
    Richard Fagen
    Aug 28, 2003
  2. Theodore H. Smith

    url typo

    Theodore H. Smith, Jul 29, 2003, in forum: XML
    Replies:
    0
    Views:
    519
    Theodore H. Smith
    Jul 29, 2003
  3. Lorenzo Villari

    K&r typo

    Lorenzo Villari, Feb 9, 2004, in forum: C Programming
    Replies:
    5
    Views:
    568
    Peter Pichler
    Feb 15, 2004
  4. mateus
    Replies:
    7
    Views:
    293
    BartlebyScrivener
    Jun 1, 2006
  5. Vassilis Rizopoulos

    Bug in Webrick::CampingHandler (typo in the code)

    Vassilis Rizopoulos, Jul 5, 2007, in forum: Ruby
    Replies:
    1
    Views:
    78
    Gregory Brown
    Jul 5, 2007
Loading...

Share This Page