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. Advertising

  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. Advertising

  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. 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. purity

    Addition with pointers

    purity, Feb 25, 2006, in forum: C++
    Replies:
    6
    Views:
    369
    Marek Vondrak
    Feb 25, 2006
  2. Replies:
    18
    Views:
    541
    Diego Martins
    Feb 26, 2007
  3. Jenny

    a program of substraction

    Jenny, Nov 1, 2008, in forum: C Programming
    Replies:
    1
    Views:
    272
    Jenny
    Nov 1, 2008
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    730
  5. sonic
    Replies:
    9
    Views:
    116
    Dr John Stockton
    May 14, 2005
Loading...

Share This Page