A different kind of subtraction ?

  • Thread starter karthikbalaguru
  • Start date
K

karthikbalaguru

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
 
K

karthikbalaguru

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
 
F

Fred

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

Keith Thompson

karthikbalaguru said:
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/>.
 
G

Guest

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
 
B

Ben Bacarisse

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top