Exercise P: Pointers
On each line, write the type and the numeric value of the
expression on the left. For pointer types, write the numeric address
(what you would get from printf("%p")
). Assume an x86-like machine
(with 32-bit addresses and integers and little endian storage). We did
line 0 for you.
Type |
Numeric value |
||
---|---|---|---|
char* p = (char*) 0x1100; |
|||
0. |
p |
|
|
1. |
&p[1] |
_________________ |
_________________ |
2. |
&p[-1] |
_________________ |
_________________ |
3. |
&p[0] |
_________________ |
_________________ |
4. |
&p[1] - &p[0] |
_________________ |
_________________ |
5. |
&p[8] |
_________________ |
_________________ |
6. |
(p + 1) - p |
_________________ |
_________________ |
7. |
&p[16] - p |
_________________ |
_________________ |
8. |
q - p |
_________________ |
_________________ |
9. |
sizeof(p) |
_________________ |
_________________ |
10. |
sizeof(*p) |
_________________ |
_________________ |
int* ip = (int*) p; |
|||
11. |
&ip[0] |
_________________ |
_________________ |
12. |
&ip[1] |
_________________ |
_________________ |
13. |
&ip[1] - &ip[0] |
_________________ |
_________________ |
14. |
(char*) &ip[1] - p |
_________________ |
_________________ |
15. |
sizeof(ip) |
_________________ |
_________________ |
16. |
sizeof(*ip) |
_________________ |
_________________ |
17. |
&ip[sizeof(int)] |
_________________ |
_________________ |
18. |
ip + sizeof(int) |
_________________ |
_________________ |
19. |
ip + 1 |
_________________ |
_________________ |
20. |
p + sizeof(int) |
_________________ |
_________________ |
int* iq = (int*) q; |
|||
21. |
iq - ip |
_________________ |
_________________ |
22. |
&iq[-1] - ip |
_________________ |
_________________ |
p[0] = p[1] = p[2] = p[3] = 0; |
|||
23. |
*ip |
_________________ |
_________________ |
*(char*) ip = 1; |
|||
24. |
*ip |
_________________ |
_________________ |
*((char*) ip + 1) = 1; |
|||
25. |
p[1] |
_________________ |
_________________ |
26. |
*ip |
_________________ |
_________________ |
*((char*) ip) = 2; |
|||
27. |
*((char*) ip) |
_________________ |
_________________ |
28. |
*ip |
_________________ |
_________________ |
Solutions
When you’re ready for solutions, go here.