Hi, I just started your book yesterday.
- Why are memory addresses for integers and floats 12 hex digits long?
- Why is the memory address for the function only 9 hex digits long?
- My Mac is a 64-bit machine. How would the memory address be different if it were 32 bits, and why?
My code:
[code]#include <stdio.h>
int main(int argc, const char * argv[]) {
int i = 17;
float j = 3.14159;
int *addressOfI = &i; //addressOfI is pointer
float *addressOfJ = &j;
printf(“i (int) stores its value at %p\n”, addressOfI);
printf(“j (float) stores its value at %p\n”, addressOfJ);
printf(“this function starts at %p\n”, main);
printf(“the int stored at addressOfI is %d\n”, *addressOfI); //dereferencing pointer
*addressOfI = 89; //change value at i’s address to 89
printf(“Now i is %d\n”, i);
printf(“An int is %zu bytes\n”, sizeof(int));
printf(“A pointer is %zu bytes\n”, sizeof(int *));
printf(“An int is %zu bytes\n”, sizeof(i));
printf(“A pointer is %zu bytes\n”, sizeof(addressOfI));
printf(“A float is %zu bytes\n”, sizeof(j));
return 0;
}[/code]
Output:
i (int) stores its value at 0x7fff5fbff86c <== 12 hex digits
j (float) stores its value at 0x7fff5fbff868 <== 12 hex digits
this function starts at 0x100000d50 <== 9 hex digits
the int stored at addressOfI is 17
Now i is 89
An int is 4 bytes
A pointer is 8 bytes
An int is 4 bytes
A pointer is 8 bytes
A float is 4 bytes
Program ended with exit code: 0
Also, this says that a 32-bit machine would have 8 hex digits. Why? (“An int is 32-bits wide.”)
http://en.wikipedia.org/wiki/Pointer_(computer_programming)#C_pointers
Example: 0x00008130 <== 8 hex digits
Please help. This is driving me crazy!