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