forked from rui314/8cc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointer.c
98 lines (87 loc) · 1.76 KB
/
pointer.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2012 Rui Ueyama. Released under the MIT license.
#include "test.h"
static void t1() {
int a = 61;
int *b = &a;
expect(61, *b);
}
static void t2() {
char *c = "ab";
expect(97, *c);
}
static void t3() {
char *c = "ab" + 1;
expect(98, *c);
}
static void t4() {
char s[] = "xyz";
char *c = s + 2;
expect(122, *c);
}
static void t5() {
char s[] = "xyz";
*s = 65;
expect(65, *s);
}
static void t6() {
struct tag {
int val;
struct tag *next;
};
struct tag node1 = { 1, NULL };
struct tag node2 = { 2, &node1 };
struct tag node3 = { 3, &node2 };
struct tag *p = &node3;
expect(3, p->val);
expect(2, p->next->val);
expect(1, p->next->next->val);
p->next = p->next->next;
expect(1, p->next->val);
}
static void t7() {
int a;
int *p1 = &a + 1;
int *p2 = 1 + &a;
expect(0, p1 - p2);
}
static void subtract() {
char *p = "abcdefg";
char *q = p + 5;
expect(8, sizeof(q - p));
expect(5, q - p);
}
static void compare() {
char *p = "abcdefg";
expect(0, p == p + 1);
expect(1, p == p);
expect(0, p != p);
expect(1, p != p + 1);
expect(0, p < p);
expect(1, p < p + 1);
expect(0, p > p);
expect(1, p + 1 > p);
expect(1, p >= p);
expect(1, p + 1 >= p);
expect(0, p >= p + 1);
expect(1, p <= p);
expect(1, p <= p + 1);
expect(0, p + 1 <= p);
expect(4, sizeof(p == p + 1));
expect(4, sizeof(p != p + 1));
expect(4, sizeof(p < p + 1));
expect(4, sizeof(p > p + 1));
expect(4, sizeof(p <= p + 1));
expect(4, sizeof(p >= p + 1));
}
void testmain() {
print("pointer");
t1();
t2();
t3();
t4();
t5();
t6();
t7();
subtract();
compare();
}