1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

pointers

Discussion in 'C' started by taps12, Jan 26, 2011.

  1. taps12

    taps12 New Member

    Joined:
    Jan 26, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    Im new to C programming and have some confusion of using pointer on arrays with this code.

    void main()
    {
    int arr[] = {10,20,30,40,50};
    int *ptr = arr;
    printf("
    %d %d ",*ptr++,*ptr);
    }

    the output is 10,10 but I think it should be 11,10.
    would appreciate if someone explains the logic of the output.
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    Why do you expect the output to be 11, 10
     
  3. taps12

    taps12 New Member

    Joined:
    Jan 26, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    the value in the array which the pointer is pointing to is being incremented by 1 by the ++ operator,so the first element is 10 in the array which the pointer is pointing to and increment by 1-->11.
     
  4. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    Then why again the second one should be 10 and not 11
     
  5. taps12

    taps12 New Member

    Joined:
    Jan 26, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    The second one is 10 because its just pointing to the first value in the array which is 10.
     
  6. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    But that you assumed increased before as well.
     
  7. taps12

    taps12 New Member

    Joined:
    Jan 26, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Ok, the printf (*ptr++,*ptr)
    now if *ptr points to 10,which is the first element in the array, then *ptr++ should be 11.

    I assumed 11 because of the (++) increment operator.*ptr remains 10.
    thats where my confusion lies I think?

    since ++ is a postfix operator ,the value of 10 is assigned first, thats the only way I can think of 10,10 output.
     
  8. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    You are viewing 10, 10 because of the parameters is passed as right to left by your compiler to the function and so after passing 10 (the right most) and then again the 10 it increments.

    There are left to right passing as well and so the statement is not very standard and is only used by some beginners and nothing more.
     
  9. Rakesh Kumar Pahwa

    Rakesh Kumar Pahwa New Member

    Joined:
    Jan 13, 2011
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    I agree with shabbir.... but one thing i want to add is *ptr++ will never increment the value pointed by the pointer ptr. It will return the value and then the pointer will be incremented or advanced to the next value in the array... if someone want to increment the value pointed to by the pointer you should use ++*ptr or (*ptr)++...
     
  10. sharma.neha22

    sharma.neha22 New Member

    Joined:
    Jan 29, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    hi
    Actually here is the issue of precedence i.e. ++(post increment operator) and *(value at address operator) both have equal priority but right to left associativity. So ++ is found at most right position and than *.

    expression is something like *(ptr++).

    now ptr is holding the base address of arr[](i.e. address of arr[0]) suppose it is 4020 after ++ it will hold the address of next element arr[1], which will 4022.But it is post increment so first assignment then increment so right now value is 4020.

    now expression will be *(4020), value at address 4020 that is 10....

    Hope you can understand now.
     
  11. Rakesh Kumar Pahwa

    Rakesh Kumar Pahwa New Member

    Joined:
    Jan 13, 2011
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    Hi Neha,

    If you go with the precedence, then postfix(++) have higher priority than indirection(*), but yes prefix(++) and indirection(*) are of equal precedence. Here the reason is not precedence but the property of postfix(++) , that is it will evaluate after the expression is evaluated. And here the expression is *ptr. That's why it first give the value and then increment, not because of precedence.

    I am not criticising you, but just wanted you to know the exact reason.

    Thanks & Regards
     
  12. sharma.neha22

    sharma.neha22 New Member

    Joined:
    Jan 29, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    hi Rakesh,

    i think u have some confusion.
    please check "Pointers in C" book. and u will get solution.
     
  13. Rakesh Kumar Pahwa

    Rakesh Kumar Pahwa New Member

    Joined:
    Jan 13, 2011
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    Hi Neha,

    I don't think there is any scope of confusion, but if you say so, I'll surely check that book.

    Thanks
     
  14. tons_astigz

    tons_astigz New Member

    Joined:
    Jan 20, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    none
    Location:
    Sagay City
    Yes, Because it is being assigned first before it is being incremented
     
  15. bangaram

    bangaram New Member

    Joined:
    Nov 9, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    i think as first arr value 10 is assigned to *ptr & *ptr value is 10; if it is declared in the next statement as *ptr++ it's value will be incremented to 11;
     
  16. bangaram

    bangaram New Member

    Joined:
    Nov 9, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    i also agree with shabbir man as it *ptr++ undergoes post increment operation first expression is evaluated then in the further step it is incremented i.e in the next statement if we write another statement k man try keeping *++ptr u will get 20,10 k have a nice day
     

Share This Page