A different kind of subtraction ?

Discussion in 'C Programming' started by karthikbalaguru, Mar 3, 2009.

  1. Hi,

    In the below code, the subtraction of 4 causes subtraction of
    8. Strange !

    int main(void)
    {
    static int x[] = {100,200,300,400,500};
    int i, *p;
    p = &x[4]-4;
    for(i=0;i<=4;i++)
    {
    printf("%d \n",*p);
    p++;
    }
    }

    The output is
    100
    200
    300
    400
    500

    But, how does p = &x[4]-4 gets evaluated to
    point to the first element of the array x.
    Shouldn't it point to 3rd element (x[2]) of
    the array x ?

    How can i actually deduct 4 and make it to
    point to x[2] (3rd element of array) ?
    Any ideas ?

    Thx in advans,
    Karthik Balaguru
    karthikbalaguru, Mar 3, 2009
    #1
    1. Advertising

  2. On Mar 4, 4:03 am, karthikbalaguru <>
    wrote:
    > Hi,
    >
    > In the below code, the subtraction of 4 causes subtraction of
    > 8. Strange !
    >
    > int main(void)
    > {
    >     static int x[] = {100,200,300,400,500};
    >     int i, *p;
    >     p = &x[4]-4;
    >     for(i=0;i<=4;i++)
    >     {
    >         printf("%d \n",*p);
    >         p++;
    >     }
    >
    > }
    >
    > The output is
    > 100
    > 200
    > 300
    > 400
    > 500
    >
    > But, how does p = &x[4]-4 gets evaluated to
    > point to the first element of the array x.
    > Shouldn't it point to 3rd element (x[2]) of
    > the array x ?
    >
    > How can i actually deduct 4 and make it to
    > point to x[2] (3rd element of array) ?
    > Any ideas ?
    >


    Oops !!
    It also has the #include<stdio.h> at the top of the file.

    Thx,
    Karthik Balaguru
    karthikbalaguru, Mar 3, 2009
    #2
    1. Advertising

  3. karthikbalaguru

    Fred Guest

    On Mar 3, 3:03 pm, karthikbalaguru <>
    wrote:
    > Hi,
    >
    > In the below code, the subtraction of 4 causes subtraction of
    > 8. Strange !
    >
    > int main(void)
    > {
    >     static int x[] = {100,200,300,400,500};
    >     int i, *p;
    >     p = &x[4]-4;
    >     for(i=0;i<=4;i++)
    >     {
    >         printf("%d \n",*p);
    >         p++;
    >     }
    >
    > }
    >
    > The output is
    > 100
    > 200
    > 300
    > 400
    > 500
    >
    > But, how does p = &x[4]-4 gets evaluated to
    > point to the first element of the array x.
    > Shouldn't it point to 3rd element (x[2]) of
    > the array x ?
    >


    Why do you think subtracting 4 from &x[4] should
    point to the 3rd element?

    x[4] is the fifth element.
    Subtracting one from its address will result in
    the address of the fourth element.

    Subtracting 2 from it will result in the address
    of the 3rd element, etc.

    --
    Fred K
    Fred, Mar 3, 2009
    #3
  4. karthikbalaguru <> writes:
    > In the below code, the subtraction of 4 causes subtraction of
    > 8. Strange !
    >
    > int main(void)
    > {
    > static int x[] = {100,200,300,400,500};
    > int i, *p;
    > p = &x[4]-4;
    > for(i=0;i<=4;i++)
    > {
    > printf("%d \n",*p);
    > p++;
    > }
    > }

    [...]

    Yes, because that's how pointer subtraction works. Read about it in
    your C textbook. (That would probably answer a great many of the
    questions you've been asking.) See also section 4 of the comp.lang.c
    FAQ, <http://www.c-faq.com/>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 3, 2009
    #4
  5. karthikbalaguru

    Guest

    On 3 Mar, 23:03, karthikbalaguru <> wrote:
    > Hi,
    >
    > In the below code, the subtraction of 4 causes subtraction of
    > 8.


    actually it's subtraction of 4 * sizeof(int) or, more generally, 4 *
    sizeof(*p)


    > Strange !


    as per standard

    > int main(void)
    > {
    >     static int x[] = {100,200,300,400,500};
    >     int i, *p;
    >     p = &x[4]-4;
    >     for(i=0;i<=4;i++)
    >     {
    >         printf("%d \n",*p);
    >         p++;
    >     }
    >
    > }
    >
    > The output is
    > 100
    > 200
    > 300
    > 400
    > 500
    >
    > But, how does p = &x[4]-4 gets evaluated to
    > point to the first element of the array x.
    > Shouldn't it point to 3rd element (x[2]) of
    > the array x ?
    >
    > How can i actually deduct 4 and make it to
    > point to x[2] (3rd element of array) ?
    > Any ideas ?


    it's useful in many cases
    , Mar 4, 2009
    #5
  6. writes:

    > On 3 Mar, 23:03, karthikbalaguru <> wrote:
    >> Hi,
    >>
    >> In the below code, the subtraction of 4 causes subtraction of
    >> 8.

    >
    > actually it's subtraction of 4 * sizeof(int) or, more generally, 4 *
    > sizeof(*p)


    OK, but I don't like thinking about it that way. When I learnt C,
    this was the way I was taught (pointers are numbers and arithmentic
    involves the size of the pointed to object). Then I moved to
    word-address machine where there were 3 kinds of pointer -- word, byte
    and function -- so some pointer conversions did arithmetic and how
    the "value" of a pointer changed depended on what kind it was. It
    became a lot clearer to think of pointers as typed. That they point
    into (sometimes imaginary) arrays, and that arithmetic on them moves
    them up and down these arrays. I wish I'd been taught that the first
    time.

    Of course, it helps in the end, to have both views, but I would have
    preferred to come across them the other way round.

    --
    Ben.
    Ben Bacarisse, Mar 4, 2009
    #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. Replies:
    3
    Views:
    15,692
    Eric Smith
    Jan 18, 2006
  2. August1
    Replies:
    4
    Views:
    341
    August1
    Sep 21, 2004
  3. Andy
    Replies:
    34
    Views:
    1,352
    Kelsey Bjarnason
    Jan 1, 2004
  4. Kevin C.

    Pointer subtraction

    Kevin C., May 7, 2004, in forum: C Programming
    Replies:
    5
    Views:
    402
  5. rsutradhar_python

    Date Subtraction

    rsutradhar_python, Jun 17, 2006, in forum: Python
    Replies:
    3
    Views:
    424
    Marc 'BlackJack' Rintsch
    Jun 18, 2006
Loading...

Share This Page