Velocity Reviews > A different kind of subtraction ?

# A different kind of subtraction ?

karthikbalaguru
Guest
Posts: n/a

 03-03-2009
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
Guest
Posts: n/a

 03-03-2009
On Mar 4, 4:03*am, karthikbalaguru <(E-Mail Removed)>
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

Fred
Guest
Posts: n/a

 03-03-2009
On Mar 3, 3:03*pm, karthikbalaguru <(E-Mail Removed)>
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

Keith Thompson
Guest
Posts: n/a

 03-03-2009
karthikbalaguru <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"

nick_keighley_nospam@hotmail.com
Guest
Posts: n/a

 03-04-2009
On 3 Mar, 23:03, karthikbalaguru <(E-Mail Removed)> 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

Ben Bacarisse
Guest
Posts: n/a

 03-04-2009
(E-Mail Removed) writes:

> On 3 Mar, 23:03, karthikbalaguru <(E-Mail Removed)> 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.