IQOD #3

Posts 1–15 of 24 · Page 1 of 2
IQOD #3
OK since we're going through these a little faster, here's another

consider the following program:
Code:
void main()
{
    int a[5] = {1,2,3,4,5};
    int*ptr = (int*)(&a+1);

   printf("%d %d", *(a+1), *(ptr-1) );
}
what is the output?
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 -->
Quote Originally Posted by B1ackAnge1 View Post
It'll definitely make it clearer there's something there..

that answer is : <!-- Incorrect -->
Haha i knew it couldn't be that easy. But now i'm pretty stumped i must admit.
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)
Quote Originally Posted by Hell_Demon View Post
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)
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!



-->
Quote Originally Posted by Hell_Demon View Post
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
Haha i was conflicted between python and perl. But either way, how much programming experience do you have hell demon?
Quote Originally Posted by zeco View Post
Haha i was conflicted between python and perl. But either way, how much programming experience do you have hell demon?
hmmm, hard to answer, i guess im getting close to intermediate-advanced
anyway im done for tonight(1:01 now ^^)
i hope to have fun with more of these tomorrow, b1ackange1, thanks for posting these. they are a lot of fun to solve
Quote Originally Posted by Hell_Demon View Post
hmmm, hard to answer, i guess im getting close to intermediate-advanced
anyway im done for tonight(1:01 now ^^)
i hope to have fun with more of these tomorrow, b1ackange1, thanks for posting these. they are a lot of fun to solve
xD i also meant, like how long have you been programming
Quote Originally Posted by zeco View Post
xD i also meant, like how long have you been programming
starting with my 7th year on 5th of october, i got a C++ book with which i started when i turned 10
Quote Originally Posted by Hell_Demon View Post
starting with my 7th year on 5th of october, i got a C++ book with which i started when i turned 10
dat verklaart men achterstand...

is het een goed boek?
gebruik je het nog? NL of EN?
Quote Originally Posted by lalakijilp View Post
dat verklaart men achterstand...

is het een goed boek?
gebruik je het nog? NL of EN?
het boek is inmiddels verouderd ^^ daarin gebruikten ze nog Microsoft Visual C++ 6.0
en het behandelde alleen de basisdingen enzo, de rest heb ik via internet geleerd(onder andere mafiacoders(R.I.P.) en hackproviders.com(niet meer wat het geweest was)
Quote Originally Posted by Hell_Demon View Post
het boek is inmiddels verouderd ^^ daarin gebruikten ze nog Microsoft Visual C++ 6.0
en het behandelde alleen de basisdingen enzo, de rest heb ik via internet geleerd(onder andere mafiacoders(R.I.P.) en hackproviders.com(niet meer wat het geweest was)
hoeland duurt het voordat je de basis goed hebt?
Posts 1–15 of 24 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us