The output is <!-- 2 1 -->
By the way, i think i'm going to put my hidden stuff in the comment style of HTML. Or maybe something else if i find a cooler comment style.
OK since we're going through these a little faster, here's another
consider the following program:
what is the output?Code:void main() { int a[5] = {1,2,3,4,5}; int*ptr = (int*)(&a+1); printf("%d %d", *(a+1), *(ptr-1) ); }
The output is <!-- 2 1 -->
By the way, i think i'm going to put my hidden stuff in the comment style of HTML. Or maybe something else if i find a cooler comment style.
It'll definitely make it clearer there's something there..
that answer is : <!-- Incorrect -->
output is 2 5
explenation:
2 because *(a+1) is the same a[1](the address of a[0]+the size of 1 int resulting in a[1])
5 because &a+1 is the first address after a and *(ptr-1) is that minus the size of 1 int resulting in the last address of a
basicly a + sizeofa - sizeof1int resulting in the address of the last int in a(which contains 5)
sorry for the crappy explenation hard to do when you're tired)
Last edited by Hell_Demon; 09-18-2009 at 05:13 PM.
Hmmm <!--i wouldn't have gotten that. That sounds right. I guess cause i didn't know that would happen. I thought it would have been just simple pointer arithmetic. Then again i suppose it was simple pointer arithmetic, if i considered the entire array was one increment. Ok, i understand =D -->
im not yet sure if I am infact right, we'll have to wait for b1ackange1 to answer, also to your signature: learn python, its awsome
<!--
This sample really shows that C++ let's you do a whole lot of stuff other
languages don't let you do by giving you access to memory you're not supposed to screw up
The first item is simple: *(a+1) would be the same as a[1] so that's the number 2;
Now for the second part: Indeed &a is a pointer to the address of an int[5]; so when we add 'one' we're adding 'one' of tne same size being int[5]; so we end up at the theoretically next int[5] item in memory, right behind a[5]; so when we then move one int* back from that we are looking at the last item in a[5] which is the number 5;
If you paste this in your compiler you will see that the address is indeed 5* the size of an int larger than &a.
With any of these it's always handy to write a small test app and pasting the code in so you can follow the program flow.
So you got it again HD! nice work!
-->