Address Printing

This is a discussion on Address Printing within the C Programming forums, part of the General Programming Boards category; Code: \*It is assumed that the addresses of the variables i, j and the first element of the integer array ...

  1. #1
    Air
    Air is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    26

    Address Printing

    Code:
    \*It is assumed that the addresses of the
    variables i, j and the first element of the integer array num in hexadecimal are
    22FF6C, 22FF68, 22FF40 respectively.*\
    
    int i, j = 3;
    int *pi = &i, *pj = &j;
    int num[5]={25, -1, 3, 12, 100};
    printf("%x, %x, %x\n", &i, &j, num);
    printf("%x, %x\n", pi, pj);
    j++;
    *pj *= 3;
    printf("%x, %d\n", pj, *pj);
    pi = num;
    num[0] += 5;
    printf("%x, %d\n", pi, *pi);
    printf("%x, %d\n", pi+j, *pi+j);
    *pj = *pj - 8;
    printf("%x, %d\n", pj, *pj);
    What is the printed in the last line. I understand that it is 4 for %d buy don't understand the address. I thought it is 22FF72 because if j=3's Address is 22FF68 then we add 4 and get 22FF72. But why is 22FF68 is the solution?

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    You are asking why the compiler is placing certain variables at certain addresses. The answer is "Because it does."

    It's no more mysterious than the fact that my favorite color is blue. Coulda' been red, coulda' been green. But it's blue. Just the way it is.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    pj is, was, and always has been 22FF68 according to your note at the top. Since you do not change it, the computer does not change it on its own volition; it stays what it is.

  4. #4
    Air
    Air is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    Quote Originally Posted by tabstop View Post
    pj is, was, and always has been 22FF68 according to your note at the top. Since you do not change it, the computer does not change it on its own volition; it stays what it is.

    But isn't j=pj=4 so the address value will change? At the top, it is 3.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    j is three, then four, then twelve. pj is, was, and will always be 22FF68. No statement in the program changes pj, so how could it change?

  6. #6
    Air
    Air is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    Quote Originally Posted by tabstop View Post
    j is three, then four, then twelve. pj is, was, and will always be 22FF68. No statement in the program changes pj, so how could it change?
    If it was *pj then would it have made a difference?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Air View Post
    If it was *pj then would it have made a difference?
    Go back to your code. In any statement you care to point at, whatever operation you have in mind, it already was *pj in your code. Asking what would be different if you used *pj doesn't make any sense, since you wouldn't change anything.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Also you need to think for five seconds what the word "address" means. If you have a house at 17 Main Street, and you tear it down and build one that is three times bigger, does your address suddenly become 51 Main Street?

  9. #9
    Air
    Air is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    Ah, thanks. It makes sense.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    Quote Originally Posted by Air View Post
    But isn't j=pj=4 so the address value will change? At the top, it is 3.
    Code:
    j = *pj;
    j != pj;

  11. #11
    Air
    Air is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    If I was just given:

    Code:
    int i=2
    Would *i equal the address of i?

    Also, if instead given:

    Code:
    int i=2
    *pi = &i
    Would pi give address of *pi which also is address of i. Also, would that mean that pi=*i?

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    Quote Originally Posted by Air View Post
    If I was just given:

    Code:
    int i=2
    Would *i equal the address of i?
    don't confuse the name of a memory location with its contents; i is an int and there's no *i.
    Quote Originally Posted by Air View Post
    Also, if instead given:

    Code:
    int i=2
    *pi = &i
    Would pi give address of *pi which also is address of i.
    yes, in a nutshell
    Quote Originally Posted by Air View Post
    Also, would that mean that pi=*i?
    again there is no *i; the variable is i and its contents are accessible directly as i and indirectly as *pi.

  13. #13
    Air
    Air is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    Thanks for all the help so far.

    Furthermore, these two lines are below the codes on my first post:
    Code:
    printf("%x, %d\n", pi+num[2], *(pi+num[2]));
    printf("%x, %d\n", num+5, *(pi+*pj));
    What value of num[2] is considered?

    EDIT: For *(pi+num[2]), what values are used? I thought it would have been 30+3 = 33 but it's 12.
    Last edited by Air; 01-15-2009 at 06:19 PM.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    Quote Originally Posted by Air View Post
    Thanks for all the help so far.

    Furthermore, these two lines are below the codes on my first post:
    Code:
    printf("%x, %d\n", pi+num[2], *(pi+num[2]));
    printf("%x, %d\n", num+5, *(pi+*pj));
    What value of num[2] is considered?

    EDIT: For *(pi+num[2]), what values are used? I thought it would have been 30+3 = 33 but it's 12.
    where do you think pi points to? you're adding the contents of num[2] to pi and then dereferencing it not the other way around.

  15. #15
    Air
    Air is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    The content of num[2] is 3, isn't it?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 10:48 AM
  2. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-16-2007, 12:48 AM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  4. printing memory at address..help!
    By myjay in forum C Programming
    Replies: 5
    Last Post: 09-21-2003, 04:58 PM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 02:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21