Thread: Understanding Output Generated

1. 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. And where is your output? You do not suppose any compiler on any OS will produce same output with the code above?

3. 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. 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. Originally Posted by Air
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...

6. Originally Posted by tabstop
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. 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. Originally Posted by tabstop
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. Originally Posted by Air
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

10. Originally Posted by vart
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. 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