]> git.sur5r.net Git - cc65/blob - test/ref/pointer2.c
Fixed a pointer-difference, comparison-with-zero expression.
[cc65] / test / ref / pointer2.c
1 /*\r
2   !!DESCRIPTION!! pointer test\r
3   !!ORIGIN!!\r
4   !!LICENCE!!     public domain\r
5 */\r
6 \r
7 #include "common.h"\r
8 #include <stdio.h>\r
9 \r
10 /*\r
11   check behaviour on incompletely declared arrays\r
12 */\r
13 \r
14 char i1[];\r
15 \r
16 void test1(void) {\r
17 int a;\r
18 \r
19         a=sizeof(i1[0]);\r
20         printf("%04x - ",a);\r
21         if(sizeof(i1[0])==sizeof(char)) {\r
22                 /* gcc gives size of element */\r
23                 printf("sizeof(i1[0]) gives size of element\n");\r
24         }\r
25         if(sizeof(i1[0])==sizeof(char*)) {\r
26                 printf("sizeof(i1[0]) gives size of pointer to element\n");\r
27         }\r
28 }\r
29 \r
30 /*\r
31   check behaviour on string init\r
32 */\r
33 \r
34 char t1[]="abcde";\r
35 char t2[]={"abcde"};\r
36 \r
37 char *t3="abcde";\r
38 char *t4={"abcde"};\r
39 \r
40 void test2(void) {\r
41 char c1,c2,c3,c4;\r
42 int i,e=0;\r
43         for(i=0;i<5;i++){\r
44                 c1=t1[i];c2=t2[i];c3=t3[i];c4=t4[i];\r
45 /*              printf("%02x %02x %02x %02x\n",c1,c2,c3,c4); */\r
46                 printf("%c %c %c %c\n",c1,c2,c3,c4);\r
47                 if(!((c1==c2)&(c1==c3)&(c1==c4))) e=1;\r
48         }\r
49         if(e) printf("test2 failed.\n");\r
50         else printf("test2 ok.\n");\r
51 }\r
52 \r
53 /*\r
54   check behaviour on extern-declarations inside functions\r
55 */\r
56 \r
57 typedef struct {\r
58   char *name;\r
59   void *func;\r
60 } A3;\r
61 \r
62 #ifdef NO_SLOPPY_STRUCT_INIT\r
63 A3 a3[] = {\r
64   { "test3", (void*) NULL },\r
65   { "test3", (void*) NULL },\r
66 };\r
67 #else\r
68 /*gcc warning: missing braces around initializer (near initialization for `a3[0]')\r
69   this type of struct-initialization seems to be kinda common */\r
70 A3 a3[] = {\r
71     "test3", (void*) NULL  ,\r
72     "test3", (void*) NULL  ,\r
73 };\r
74 #endif\r
75 \r
76 void test3a(A3 *list, int number){\r
77         printf("%s %d\n",list->name,number);\r
78 }\r
79 \r
80 static void test31(void)\r
81 {\r
82     extern A3 a3[];\r
83     test3a(a3, -1);\r
84 }\r
85 \r
86 #if 0\r
87 /* this variation compiles and works with cc65, but gives an error with gcc :=P */\r
88 static void test32(void)\r
89 {\r
90     extern A3 *a3;\r
91     test3a(a3, -1);\r
92 }\r
93 #endif\r
94 \r
95 static void test30(void)\r
96 {\r
97     test3a(a3, -1);\r
98 }\r
99 \r
100 /*\r
101   todo: add test on function pointers in the form of (*func)(arg) ...\r
102   cc65 seems to have problems here aswell ;/\r
103 */\r
104 \r
105 int main(void) {\r
106         test1();\r
107         test2();\r
108         test30();\r
109         test31();\r
110 /*      test32(); */\r
111         return 0;\r
112 }\r