Monday 15 July 2013

c - int a[20], what's the answer of " &a + 4"? -


  int a [20];   

Let's assume that [20] has a 100 address in memory. The size of the int is 4. It's easy to know that a = 100, and a = 100, & amp; A [4] = 116 . But when I try (and one + 4), then the answer is 420 (I test it in GCC, Dev-C, VC) I think why & amp; A + 4 = 420 is 420 = 100 + 4 * size (one [20]) = 100 + 4 * (4 * 20)

(above " = "" Equal "means)

Is that correct?

Strictly speaking, the answer is that the behavior is undefined.

& amp; A array of address 1 is added to an address (indicator value) by the type of shape that it shows that but the pointer arithmetic only , when the result points to the element of the same array as the original pointer, or at the end of it is just past (for the objectives of the indicator digit, an object is considered as the array of an element .)

If you have a certain " Well-behaved "Memory model is related to space and address sensor addressing a single linear unbroken page, then your assumptions ( & amp; a is 100, sizeof (int) == 4 ), then yes, the result will be and a + 4 of 420 would be more accurate, since 420 is an integer, one Not indicative, this would be (int (*) [10]) 420 - again, behave well in order to handle conversions between pointers and integers Are there.

No comments:

Post a Comment