suprising output !!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • onkar

    suprising output !!


    void print(int *a,int *b,int *c,int *d,int *e){
    printf("++ptr=% d\nptr--=%d\nptr=%d\npt r++=%d\n++ptr=% d
    \n",*a,*b,*c,*d ,*e);
    }
    int main(void){
    static int arr[]={97,98,99,100, 101,102,103,104 };
    int *ptr=arr+1;
    print(++ptr,ptr--,ptr,ptr++,++pt r);
    return 0;
    }


    gives me ::

    ++ptr=100
    ptr--=100
    ptr=100
    ptr++=99
    ++ptr=100

    I am totally foxed by this answer. Please try to explain why the
    answer must be like that ??

  • Kenneth Brody

    #2
    Re: suprising output !!

    onkar wrote:
    [...]
    print(++ptr,ptr--,ptr,ptr++,++pt r);
    [...]
    I am totally foxed by this answer. Please try to explain why the
    answer must be like that ??
    For the exact same reasons given in your "tricky output" thread from
    last week. You have invoked undefined behavior by modifying ptr
    more than once between sequence points.

    (And note that there is no "comma operator" in the above statement,
    which would have been a sequence point.)

    And note that the output doesn't "must be" like that. It happens to
    be that way on your particular compiler, with the particular set of
    options you compiled it. With UB, _anything_ could happen.

    My compiler, with default settings, gives:
    ++ptr=100
    ptr--=99
    ptr=99
    ptr++=99
    ++ptr=99
    With optimization on, it gives:
    ++ptr=100
    ptr--=99
    ptr=100
    ptr++=99
    ++ptr=100
    Note that _both_ sets of output are "correct" as far as the Standard
    is concerned.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>

    Comment

    • Peter Nilsson

      #3
      Re: suprising output !!

      onkar <onkar....@gmai l.comwrote:
      void print(int *a,int *b,int *c,int *d,int *e){
              printf("++ptr=% d\nptr--=%d\nptr=%d\npt r++=%d
      \n++ptr=%d\n",* a,*b,*c,*d,*e); }
      >
      int main(void){
              static int arr[]={97,98,99,100, 101,102,103,104 };
              int *ptr=arr+1;
              print(++ptr,ptr--,ptr,ptr++,++pt r);
              return 0;
      }
      >
      gives me ::
      >
      ++ptr=100
      ptr--=100
      ptr=100
      ptr++=99
      ++ptr=100
      >
      I am totally foxed by this answer.
      Why do you expect to receive a different answer to
      same question you asked a few days ago?

      Tell us what aspects of previous answers (e.g. the FAQ)
      are still causing confusion.

      --
      Peter

      Comment

      • santosh

        #4
        Re: suprising output !!

        onkar wrote:

        <snip the usual>

        You *are* a troll, aren't you?

        Comment

        Working...