neel
neel

Reputation: 9061

In C, how does arithmetic between a pointer and an array work?

What should be the value of y and why?

int x[] = { 1, 4, 8, 5, 1, 4 }; 
int *ptr, y; 
ptr  = x + 4; 
y = ptr - x;  

I think y should be 4*sizeof(int), but it is giving 4. Why ?

Upvotes: 5

Views: 10752

Answers (4)

Mike
Mike

Reputation: 49403

I think y should be 4*sizeof(int)

Good thinking, and guess what? It is giving 4*sizeof(int), but you're not looking at it right. ;)

When you're playing with pointers, you're looking at addresses, so let's check out some addresses

int x[] = { 1, 4, 8, 5, 1, 4 };

//Just for fun, what is the address of each element in the array?
printf("%#x, %#x, %#x, %#x, %#x, %#x\n", x+0, x+1, x+2, x+3, x+4, x+5);

ptr = x + 4;

printf("%#x - %#x\n", ptr, x);  // Give us the address of ptr in hex
                                // and give us the address of x
y = ptr - x;                    

printf("%d\n", y);

Output:

   x[0]         x[1]        x[2]        x[3]         x[4]       x[5]
0xbf871d20, 0xbf871d24, 0xbf871d28, 0xbf871d2c, 0xbf871d30, 0xbf871d34

   ptr           x
0xbf871d30 - 0xbf871d20

4

So ptr is x+4 (which is really x + 4*sizeof(int) or x+16 in your case). And we're going to subtract from that x or the base address, so the actual math is 0x30 - 0x20 = 0x10 or in dec 16.

The reason you're seeing 4 on the output is because the compiler knows you're doing operations on int * so it's dividing that 16 by sizeof(int) for you. Nice hm?

If you want to see the actual value you need to do something like this:

int one, two;
...
one = (int)ptr;  //get the addresses, ignore the "type" of the pointer 
two = (int)x;
y = one - two;

Now y will give you 0x10(hex) or 16(dec)

Upvotes: 8

bali
bali

Reputation: 53

if the operands of the binary '-'
are both pointers of same type it is evaluated as

(p-q)/sizeof(type of p or q)

in case the second operand is integer
then

p - sizeof(p)*q

Upvotes: 2

Preet Sangha
Preet Sangha

Reputation: 65496

Just apply some simple algebra

if
    ptr = x + 4
and
    y = ptr - x

therefore

y = (x + 4) - x

hence y = 4 + x - x

thus y = 4 + 0 

     y = 4

Edit: Addressing the comment

This is C, a ptr is just value of whatever size bits. Adding a number to it (except in the case of overflow) is just some integral number + another integral number (cast to the appropriate size), and thus removing the original number leaves a remainder. Since we only added 4 (smaller than an int) this means that there is no issue implicitly casting it to the y int.

Upvotes: 4

Maksim Skurydzin
Maksim Skurydzin

Reputation: 10541

It should be the number of int's between the address that points to the start of x and the address of x's 4-th element => 4.

From c99 standard:

6.5.6 Additive operators

9/ When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; the result is the difference of the subscripts of the two array elements.

To find out more, try searching for pointer arithmetic.

Upvotes: 7

Related Questions