Thread: IQOD #3

Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical

    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?

  2. #2
    zeco's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    Canada
    Posts
    683
    Reputation
    12
    Thanks
    78
    My Mood
    Cynical
    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.

  3. #3
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical
    It'll definitely make it clearer there's something there..

    that answer is : <!-- Incorrect -->

  4. #4
    zeco's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    Canada
    Posts
    683
    Reputation
    12
    Thanks
    78
    My Mood
    Cynical
    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.

  5. #5
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    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.

  6. #6
    zeco's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    Canada
    Posts
    683
    Reputation
    12
    Thanks
    78
    My Mood
    Cynical
    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 -->

  7. #7
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    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

  8. #8
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical
    <!--

    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!



    -->

  9. #9
    zeco's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    Canada
    Posts
    683
    Reputation
    12
    Thanks
    78
    My Mood
    Cynical
    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?

  10. #10
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    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

  11. #11
    zeco's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    Canada
    Posts
    683
    Reputation
    12
    Thanks
    78
    My Mood
    Cynical
    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

  12. #12
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    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

  13. #13
    lalakijilp's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    310
    Reputation
    9
    Thanks
    53
    My Mood
    Blah
    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?

  14. #14
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    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)

  15. #15
    lalakijilp's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    310
    Reputation
    9
    Thanks
    53
    My Mood
    Blah
    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?

Page 1 of 2 12 LastLast

Similar Threads

  1. IQOD #5
    By B1ackAnge1 in forum C++/C Programming
    Replies: 6
    Last Post: 09-21-2009, 03:33 PM
  2. IQOD #4
    By B1ackAnge1 in forum C++/C Programming
    Replies: 7
    Last Post: 09-20-2009, 01:37 PM
  3. IQOD #2
    By B1ackAnge1 in forum C++/C Programming
    Replies: 37
    Last Post: 09-19-2009, 04:40 PM
  4. Extra Credit IQOD #2A
    By B1ackAnge1 in forum C++/C Programming
    Replies: 18
    Last Post: 09-18-2009, 06:39 PM
  5. Interview Question of the Day (IQOD) #1
    By B1ackAnge1 in forum C++/C Programming
    Replies: 26
    Last Post: 09-18-2009, 04:24 PM

Tags for this Thread