-
Notifications
You must be signed in to change notification settings - Fork 220
/
pointerfun-output
60 lines (49 loc) · 2.98 KB
/
pointerfun-output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
We'll be running a series of experiments, and printing the results
to try to better understand pointers and memory in C. Ready?
The memory address of global variable globalvar is 0x1099a5040
The memory address of static variable staticvar is 0x1099a5048
The memory address of local variable a is 0x7fff5625c844
The memory address of local variable b is 0x7fff5625c840
ptr_to_a has the address 0x7fff5625c838, and the content 0x7fff5625c844. *ptr_to_a = 4
ptr_to_ptr_to_a has the address 0x7fff5625c830, and the content 0x7fff5625c838. *ptr_to_ptr_to_a = 0x7fff5625c844. **ptr_to_ptr_to_a = 4
Mallocing *c to sizeof(int)*5; *d to sizeof(int)
The memory address on the heap int *c points to is 0x7fe3b8c03ab0
The memory address on the heap int *d points to is 0x7fe3b8c03ad0
But note that c itself is in the address 0x7fff5625c828. 0x7fff5625c828 -> 0x7fe3b8c03ab0, which contains 9
Look at all the different ways we can use pointers to a block of malloc'd memory
Now looping through c+0...c+4:
*(c+0) == 9 c[0] == 9 0[c] == 9
*(c+1) == 10 c[1] == 10 1[c] == 10
*(c+2) == 11 c[2] == 11 2[c] == 11
*(c+3) == 12 c[3] == 12 3[c] == 12
*(c+4) == 0 c[4] == 0 4[c] == 0
one last thing, carr is an int array of size 5. Let's do what we did with c again with carr...
Now looping through carr+0...carr+4:
*(carr+0) == 9 carr[0] == 9 0[carr] == 9
*(carr+1) == 10 carr[1] == 10 1[carr] == 10
*(carr+2) == 11 carr[2] == 11 2[carr] == 11
*(carr+3) == 12 carr[3] == 12 3[carr] == 12
*(carr+4) == 0 carr[4] == 0 4[carr] == 0
null pointers are just pointers whose value is 0 (or in hex 0x0)
e = NULL. Its value is 0x0
e = 0. Its value is still 0x0
e = '\0'. Its value is still 0x0
null characters are the number 0. 0 == '\0'
f = '0'. Its value as a char is 0, but as a number is 48
f = 0. Its value as a char is , but as a number is 0
f = 'a'. Its value as a char is a, but as a number is 97
Now let's play with functions!
The memory address of the function add is 0x1099a4530
The memory address of the function multiply is 0x1099a4580
We'll try to add, by asking the operate function to use the add function...
We'll add a + b and store into d
(inside operate) Storing into 0x7fe3b8c03ad0 results of operation at memory address 0x1099a4530
(inside add) Adding the contents of 0x7fff5625c844 and 0x7fff5625c840. (4+3)
The contents of malloc'd space 0x7fe3b8c03ad0, d, is 7
Ok, now let's try to multiply, by asking the operate function to use the multiply function...
We'll multiply a * b and store into globalvar, but use *ptr_to_ptr_to_a, rather than &a
(inside operate) Storing into 0x1099a5040 results of operation at memory address 0x1099a4580
(inside multiply) Multiplying the contents of 0x7fff5625c844 and 0x7fff5625c840. (4*3)
The contents of global variable 0x1099a5040, globalvar, is 12
"This is a string literal!" is acessed via char *sl at 0x1099a4e5e
The last character of ^ is '!', stored at 0x1099a4e76