The difference between...
-
Hi all, This may seem very elementary, but why "&a + 1" and "a + 1" are different in the following code
int a\[5\] = {1, 2, 3, 4, 5}; int\* ptr = (int\*)(&a + 1); printf("%d, %d", \*(a + 1), \*(ptr-1));
Thanks,
*(a + 1)
returns a pointer to the 2nd element in a, which here is 2;a + 2
would return the 3rd element in a, which is 3, and so on.(&a + 1)
returns&a
, which is a pointer ofa
, and as pointer arithmetic is used here,+1
increments this address by 20, asa
has a size of 20 (5 ints or 5 * 4 bytes), which would point to the next memory block AFTERa
. When you print(ptr-1)
this decreases the pointer address by 4 (4 bytes to an int), which returns the 5th element in the array, which is 5. Hope this helps, --Perspx"The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript
-
Hi all, This may seem very elementary, but why "&a + 1" and "a + 1" are different in the following code
int a\[5\] = {1, 2, 3, 4, 5}; int\* ptr = (int\*)(&a + 1); printf("%d, %d", \*(a + 1), \*(ptr-1));
Thanks,
So, what was the question? BTW try also
printf("%d, %d\n", sizeof(a), sizeof(int*));
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[Image resize DLL] -
*(a + 1)
returns a pointer to the 2nd element in a, which here is 2;a + 2
would return the 3rd element in a, which is 3, and so on.(&a + 1)
returns&a
, which is a pointer ofa
, and as pointer arithmetic is used here,+1
increments this address by 20, asa
has a size of 20 (5 ints or 5 * 4 bytes), which would point to the next memory block AFTERa
. When you print(ptr-1)
this decreases the pointer address by 4 (4 bytes to an int), which returns the 5th element in the array, which is 5. Hope this helps, --Perspx"The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript
-
I still don't understand. To me "a" is of type a[5] and "&a" is of type pointer, so they're supposed to calculated in 20 and 4 bytes unit respectively, but that's contrary to the actual result.
I understand your doubts. Arrays on stack and dynamically allocated arrays have differences, but we must have a uniform way to access elements with pointers. From this point of view 'a' must be an int*. On the other hand 'a' must be like any other stack object (struct or class). We must be able to take the actuall size with the sizeof operator or access the elements by casting the base address '&a'. For reasons like the above we see 'strange' things like &a == a
-
I still don't understand. To me "a" is of type a[5] and "&a" is of type pointer, so they're supposed to calculated in 20 and 4 bytes unit respectively, but that's contrary to the actual result.
a is an array of 5 ints. so
a
is the address of the 1st element of the array, and is of type int* (if we consider an int to be 4 bytes, incrementing a will move the pointer 4 bytes further).&a
however is the address of the variable a itself, and is of type int[5] (if we consider an int to be 4 bytes, int[5] weights 20 bytes, so incrementing &a will move the pointer 20 bytes further). as suggested by CPallini, have you tried to print some sizeof to check that ?[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]