1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice


Discussion in 'C' started by swapnaoe, Mar 12, 2007.

  1. swapnaoe

    swapnaoe New Member

    Hi ALL,
    When I run the follwoing code, I get the follwoing
    I tried changing the value of 'a' and the last line of the code, the printf prints whatever is assigned to 'a'. Please explain me this behaviour.
    void main()
    	int a=6;
    	int b=1024;
    	int *swap=(&b-1);
    Last edited by a moderator: Mar 12, 2007
  2. shabbir

    shabbir Administrator Staff Member

    You are printing the address of b which is in your system 1245048 and swap which is 4 bytes less which should be 1245044 and swap after adding 2 should be 1245044 + 2 = 1245052 which is what is expected.
  3. swapnaoe

    swapnaoe New Member

    Yes u r right.. then
    what is *swap in the last line???
    The value present in 1245052 at that point of time.. right???
    If u can run the program in ur system.. u can find that, the value is nothing but 'a', in this case 6.
    If u change the value of 'a' then the output also changes..
    Any idea fro this type of behaviour??
  4. DaWei

    DaWei New Member

    swap is set to the address of the integer before b (&b minus 1), in this case, a. Therefore, when you print what swap points to (a), you get the contents (a). This only works because your machine happens to have a common implementation of the stack. There is no requirement of the language that requires it to be so.
  5. swapnaoe

    swapnaoe New Member

    Mr DaWei
    Let us consider the follwoing example:
    swap=&b-1, swap=1245044(According to ur explanation)
    swap+=2, swap=swap+2, swap=1245052(if int occupies 4 bytes)
    *swap= contents(1245052), swap= unknown value not 'a'.
    Please correct me if i am wrong

  6. wrecker

    wrecker New Member

    Quite simple, should be no confusion. Just pointer manipulation. Understand whats actually procedure of pointer storage and you will make it. Or take a K&R and make your task simple.....
  7. swapnaoe

    swapnaoe New Member

    Could you please go through the discussion i had with Dawei!!!
  8. DaWei

    DaWei New Member

    It's almost certainly the case that your system is implemented in such a way that locals and automatic variables are allocated on the stack, and this in a system, such as the x86, where the stack grows downward. Presuming that 'a' is located at 1245412, in your case, then the variables are at the following addresses:
    1245412 a	6
    1245408 b	1024
    1245404 swap	1245404 (&b - 1*sizeof int)
    swap then becomes, after += 2*sizeof int, 1245404 + 8, 1245412, which is the address of a
    when dereferenced, then, the value of a (6) is printed. Your number, 1245052, seems to be a typo.

    Again, this is not only implementation dependent, hardware-wise, but also, software-wise. Exercises like this are only useful when produced on the same system with the same OS and the same compiler. Just a caveat.
  9. swapnaoe

    swapnaoe New Member

    Thank you guys.. its clear now.. :)

Share This Page