-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathkoan.c
70 lines (53 loc) · 1.62 KB
/
koan.c
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
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main() {
puts("This is the C++ pointer koan.");
puts("Meant to practice pointer arithmetic");
puts("It simply uses assert on a known data structure, and you fill in the rest");
// Start with operations on an unsigned char - where a char is 1 byte
unsigned char originalPointer[256];
for(int i = 0; i < 256; i++)
originalPointer[i] = i;
unsigned char *p = originalPointer;
assert(p[0] == 0);
assert(*p == 0);
p = p + 3;
assert(*p == 0);
unsigned char a = *p++;
assert(a == 0);
assert(*p == 0);
unsigned char b = *(p++);
assert(b == 0);
assert(*p == 0);
// Would love to deal with the 'endianness' of the memory.
// Not sure I can
unsigned char c = *++p;
assert(c == 0);
assert(*p == 0);
// Sure you can - cast and display the hex value
// but how would you simulate the memory?
unsigned char *np = p + 3;
assert(*np == 0);
assert(*p == 0);
//Let's setup an integer pointer
unsigned int *intP = (unsigned int*) originalPointer;
// Hint remember your processors Endianness
// Also you should probably do these as hex
assert(*intP == 0x00000000);
intP = intP + 3;
assert(*intP == 0x00000000);
intP = (unsigned int *)((unsigned char *) (intP) + 3);
assert(*intP == 0x00000000);
unsigned int intX = *intP--;
assert(intX == 0x00000000);
assert(*intP == 0x00000000);
unsigned int *subtractedP = intP - 1;
assert(*subtractedP == 0x00000000);
assert(intP - subtractedP == 0);
// Obscure C trick
intP = (unsigned int*) originalPointer;
assert(3[intP] == 0x00000000);
puts("Congrats you did it! You must know your pointers");
return 0;
}