Thread: Problem in recursion

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    8

    Problem in recursion

    hey everyone....i ws givn a task to find out bout a prblem in recusion...!!!! so here it goes....:-
    Code:
      main()                                                 
    { 
                                                                                                           int a;
                                                                                                    printf("address of a is: %u",&a);                                                    
            func2(); 
                                                                                         }                                                                                                  
    func2()
                                                                                              { 
                                                                                                            int a;
                       printf("add of a is %u",&a);
           }
    so d ques ws if both d addressed vll b same or different?????? my ans ws its differnt bt it actually comes out to be same.........any reason y is it so??????????!!!!!!!!! anyone????

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    1

    Smile

    sorry but could you rewrite your code more neatly?

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    Quote Originally Posted by zchking View Post
    sorry but could you rewrite your code more neatly?
    Agreed! If you can also rewrite the question, reading SMS language gives me headache!

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    oops sorry....actually i m new here.....!!!! so got a bit messed up.....!!!!!!!!!
    here it goes.....
    Code:
    main()
    {
    int a;
    printf("address of a is %u",&a);
    func2();
    }
    func2()
    {
    int a;
    printf("address of a is %u",&a);
    }
    will both the addressed dats being printed is same or differnt.....????
    n olso why is it so???

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Annie!

    I'd ditch the slang. It doesn't go over well here.

    Is %u used to print up addresses? Better check that format for addresses.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    yup...... %u is format specifier for unsigned integer values......n since address is non negative.....v use %u for address.....at lest dats wat v hav been doin it in colg.....n thanx 4 d welcom....

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    actually.....its correct n its even giving d result.....its printing d same address in both the cases......i was just wondering why is it so????

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    %p: Print a pointer value (the pointer, not what it points to), in some implementation-defined format. The corresponding argument is a void *. Usually, the value printed is the numeric value of the pointer--that is, the memory address pointed to--in hexadecimal. For the segmented architecture of the IBM PC, pointers are often printed using a segmentffset notation.
    Is %p the same on your system as %u?

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    i use it in linux.......n ol of us actually use %u....anyways wats %p....neva used dat???

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    %p is the hexadecimal representation of the address.And you will get ans as hex numbers.actually it gives different address..but in turbo c you will get same address for both..but in linux u will not..

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    %u if for unsigned integers. Use %p for pointers. Or you can cast the pointer to an unsigned int, then use %u. Otherwise, the behavior is undefined.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    the variable 'a' in main and func2 are different variables with different lifetimes. it is not always helpful to try to impose a mental model on what the compiler is doing under the hood, because it is complicated. whether you use %u or %p, the address printed may or may not be the same depending on the stack layout and calling convention of your compiler. but since the 'a' in main is not used after the printf, the compiler might reuse that location on the stack. it also depends on whether or not you enable optimization.

  13. #13
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    bt it comes out to be the same always.......is this explaination correct???? dat since both the "a" are local variabe.....it gets stored in stack area in memory...... so wen i first define "a" in in main.....its first stored(pushed) in top position(being a stack) and after dat its automatically popped out of the stack once i com out of main.....after that in func2......again it is pushed at top hence both has got the same address....???????????

  14. #14
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by annie View Post
    bt it comes out to be the same always.......is this explaination correct???? dat since both the "a" are local variabe.....it gets stored in stack area in memory...... so wen i first define "a" in in main.....its first stored(pushed) in top position(being a stack) and after dat its automatically popped out of the stack once i com out of main.....after that in func2......again it is pushed at top hence both has got the same address....???????????
    He just said that forming a mental model (at least in this stage) isn't helpful, did you not read his post?

    Also, ditch the ....ing slang talk. No one wants to read it, it looks incredibly trashy, and you're not going to get help if you continue.

    But, to answer your question, most computers will make them the same, because that's the space it reserves for newly defined 32-bit types.

  15. #15
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    i appreaciate the fact that you guys hv helped me a lot in this......but let me clear out something....i DID NOT use any slangs here......mind it.......and as far as the question of reading the post is concerned........i hav already mentioned it clearly that i m given the task of finding out why it actually happens.....!!!!!!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursion problem
    By claudiat13 in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2010, 10:26 PM
  2. recursion problem
    By brianptodd in forum C++ Programming
    Replies: 11
    Last Post: 11-18-2003, 12:05 PM
  3. Recursion problem
    By Lost__Soul in forum C Programming
    Replies: 7
    Last Post: 04-19-2003, 04:55 PM
  4. recursion - problem.
    By Vber in forum C Programming
    Replies: 6
    Last Post: 02-24-2003, 04:57 PM
  5. recursion problem!!
    By newbie_grg in forum C++ Programming
    Replies: 5
    Last Post: 07-30-2002, 01:37 PM

Tags for this Thread