5

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 ?

3
  • 7
    Why don't you compile it and run it? Commented Oct 23, 2012 at 12:28
  • 2
    I ran it and I got y as 4 but y is it giving 4, I dont understand Commented Oct 23, 2012 at 12:30
  • 2
    I thought it was a good question....? Commented Oct 23, 2012 at 12:32

4 Answers 4

8

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)

Sign up to request clarification or add additional context in comments.

Comments

7

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.

Comments

4

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.

3 Comments

You lost me further in that second paragraph. To rephrase - if I print out the value of 'x' and the value of 'ptr', the difference is greater than 4. Yet x - ptr == 4. It's not just adding a number and taking it away again.
+1 The same explanation was running through my mind. The pointer arithmetic is not needed here to be considered. It's just set of simple equations problem.
@sje397 - yes I believe you are just doing integer maths. The pointer aspect IMHO is just a red herring. Of course the C specs could be specific (one hopes) and have this as a particular use case but because it's just C and you're using a number (4) much lower than MAX_INT I can't see how this is nothing more than a implicit integer calc.
2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.