Pointers

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

  1. swapnaoe

    swapnaoe New Member

    Joined:
    Oct 26, 2006
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Hi ALL,
    When I run the follwoing code, I get the follwoing
    1245048
    1245044
    1245052
    6
    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.
    Code:
    void main()
    {
    	int a=6;
    	int b=1024;
    	int *swap=(&b-1);
    	printf("%u\n",&b);
    	printf("%u\n",swap);
    	swap+=2;
    	printf("%u\n",swap);
    	printf("%d\n",*swap);
    }
    Regards,
    Swapna
     
    Last edited by a moderator: Mar 12, 2007
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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

    Joined:
    Oct 26, 2006
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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

    Joined:
    Oct 26, 2006
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Mr DaWei
    Let us consider the follwoing example:
    &a=1245044
    &b=1245048
    Now,
    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

    Regards,
    Swapna
     
  6. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0


    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

    Joined:
    Oct 26, 2006
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Wrecker,
    Could you please go through the discussion i had with Dawei!!!
    Swapna
     
  8. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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:
    Code:
    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

    Joined:
    Oct 26, 2006
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Thank you guys.. its clear now.. :)
    Swapna
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice