addition/substraction of pointers while passing to a function

Discussion in 'C Programming' started by udayan.singh@gmail.com, Jan 19, 2007.

  1. Guest

    Hi ,

    tried this prog .. need a clarification..

    --------------------------------------------------------------------------

    [root@localhost chap02]# cat a3.c

    #include <stdio.h>

    void print(int *a, int *b, int *c, int *d , int *e)
    {
    printf("\n %d %d %d %d %d \n", *a,*b, *c, *d , *e);
    }

    int main()
    {
    static int arr[] = {97, 98, 99, 100, 101, 102, 103, 104};

    int *ptr = arr + 1;

    print(++ptr, ptr--, ptr, ptr++ , ++ptr);

    return 0;
    }


    --------------------------------------------------------------------------
    i expected the output to be (reason being that the computation takes
    place from right extreme of the function - i think like that, might b i
    m wrong)

    100 100 100 99 99
    --------------------------------------------------------------------------

    but the actual output is :
    100 100 100 99 100

    --------------------------------------------------------------------------

    pls explain!!!!!

    tia,
    Udayan
     
    , Jan 19, 2007
    #1
    1. Advertisements

  2. wrote:

    > int *ptr = arr + 1;
    >
    > print(++ptr, ptr--, ptr, ptr++ , ++ptr);


    > pls explain!!!!!


    You are invoking undefined behavior. You are attempting to modify ptr 4
    times without an intervening sequence point. Additionally, even if that
    were not the case, you have no guarantee as to the order in which
    function arguments are evaluated. The only guarantee that you have is
    that they will all have been evaluated before the actual function call
    is made.


    --
    Clark S. Cox III
     
    Clark S. Cox III, Jan 19, 2007
    #2
    1. Advertisements

  3. wrote:

    > print(++ptr, ptr--, ptr, ptr++ , ++ptr);


    Doesn't anyone check the FAQ or follow the newsgroup before posting
    their tired, wearisome, and flat-out erroneous crap here?

    > i expected the output to be


    All possible expectations are wrong, but your incorrect expectation
    _could_ happen.
     
    Martin Ambuhl, Jan 19, 2007
    #3
  4. wrote:

    > tried this prog .. need a clarification..


    > print(++ptr, ptr--, ptr, ptr++ , ++ptr);


    > pls explain!!!!!


    u shld use r3al wrds & nt txt msg spk. thn, r3ad teh FAQ:
    http://c-faq.com/expr/evalorder1.html (r3ad 3.2 & 3.3 also). ur dbts
    will b answ3rd!!!1!!1!!

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Jan 19, 2007
    #4
  5. Guest

    Well Thanks a lot :). Sorry for reposting a tierd, message again..
    I would surely go through the mentioned list before posting again
    here..

    Regards,
    Udayan

    Christopher Benson-Manica wrote:
    > wrote:
    >
    > > tried this prog .. need a clarification..

    >
    > > print(++ptr, ptr--, ptr, ptr++ , ++ptr);

    >
    > > pls explain!!!!!

    >
    > u shld use r3al wrds & nt txt msg spk. thn, r3ad teh FAQ:
    > http://c-faq.com/expr/evalorder1.html (r3ad 3.2 & 3.3 also). ur dbts
    > will b answ3rd!!!1!!1!!
    >
    > --
    > C. Benson Manica | I *should* know what I'm talking about - if I
    > cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
     
    , Jan 20, 2007
    #5
    1. Advertisements

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. Phil
    Replies:
    1
    Views:
    773
    llewelly
    Sep 16, 2003
  2. Peter Goddard

    void pointers & void function pointers

    Peter Goddard, May 16, 2005, in forum: C Programming
    Replies:
    3
    Views:
    711
    Peter Goddard
    May 16, 2005
  3. purity

    Addition with pointers

    purity, Feb 25, 2006, in forum: C++
    Replies:
    6
    Views:
    457
    Marek Vondrak
    Feb 25, 2006
  4. Replies:
    18
    Views:
    668
    Diego Martins
    Feb 26, 2007
  5. Jenny

    a program of substraction

    Jenny, Nov 1, 2008, in forum: C Programming
    Replies:
    1
    Views:
    316
    Jenny
    Nov 1, 2008
  6. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    962
  7. Richard A. DeVenezia
    Replies:
    4
    Views:
    326
    Lasse Reichstein Nielsen
    Sep 5, 2003
  8. sonic
    Replies:
    9
    Views:
    252
    Dr John Stockton
    May 14, 2005
Loading...