Thread: Understanding Output Generated

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    26

    Understanding Output Generated

    Hello, I'm slightly confused with the concept of Hexadecimal. 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.

    Code:
    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);
    How does the output change from 22FF40 to 22FF70? I don't understand that part. Could someone explain. Thanks in advance, Air.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And where is your output? You do not suppose any compiler on any OS will produce same output with the code above?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    I know, but the data is given.

    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.
    I would be greatful if anyone could explain the change from 22FF40 to 22FF70.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You mean there where you do pi+j at the bottom? You're adding to a value and you don't understand why it's bigger? Specifically, j is 12 at this point in the program, and 12 addresses past 22FF40 is 22FF70.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Air View Post
    I know, but the data is given.



    I would be greatful if anyone could explain the change from 22FF40 to 22FF70.
    You forgot to tell where this change occured. I do not see the 22ff70 anywhere...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    Quote Originally Posted by tabstop View Post
    You mean there where you do pi+j at the bottom? You're adding to a value and you don't understand why it's bigger? Specifically, j is 12 at this point in the program, and 12 addresses past 22FF40 is 22FF70.
    Yes, I understand that j is 12 at this point. But when you mean 12 address, how is it added?

    22FF40 + 12 = 22FF(40+12) = 22FF52?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    pi points to an int, ints take up four bytes of room. So after 22FF40, the next (int) address is 22FF44, then 22FF48, then 22FF4C, etc.

    (If it was chars, then you would only add 12, since chars take up only one byte. Of course 22FF40 plus twelve is 22FF4C, not 22FF52.)

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    Quote Originally Posted by tabstop View Post
    pi points to an int, ints take up four bytes of room. So after 22FF40, the next (int) address is 22FF44, then 22FF48, then 22FF4C, etc.

    (If it was chars, then you would only add 12, since chars take up only one byte. Of course 22FF40 plus twelve is 22FF4C, not 22FF52.)

    Which letter is last? From 22FF48, it goes to 49, 4A, 4B, 4C. Which is the last letter?

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Air View Post
    Which letter is last? From 22FF48, it goes to 49, 4A, 4B, 4C. Which is the last letter?
    It is not a letter - it is hexamal digit

    A = 10
    B = 11
    C = 12
    D = 13
    E = 14
    F = 15
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    Quote Originally Posted by vart View Post
    It is not a letter - it is hexamal digit

    A = 10
    B = 11
    C = 12
    D = 13
    E = 14
    F = 15

    Aah, I see. 15 is the last? Then I assume it goes to: 22FF50, 22FF51, 22FF52, 22FF53, 22FF54, 22FF55, 22FF56, 22FF57, 22FF58, 22FF59, 22FF5A, 22FF5B, 22FF5C, 22FF5D, 22FF5E, 22FF5F, 22FF60

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    Just did it manually. Worked out F=15 is last. Thank you both for taking time out to help me. You have both been very clear and patient. Thanks once again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM