# A different kind of subtraction ?

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

1. ### karthikbalaguruGuest

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 ?

Karthik Balaguru

karthikbalaguru, Mar 3, 2009

2. ### karthikbalaguruGuest

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

3. ### FredGuest

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
4. ### Keith ThompsonGuest

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++;
> }
> }

[...]

your C textbook. (That would probably answer a great many of the
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
5. ### 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
6. ### Ben BacarisseGuest

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