-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtconst2.c
147 lines (115 loc) · 2.33 KB
/
tconst2.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int
myfun(int lines,
const int array[][6])
{
int sum = 0;
int i, j;
#if 0
array[0][0] = 1; /* read-only */
memset(array, '\0', lines * 6 * sizeof(array[0][0]));
#endif
for (i = 0; i < lines; i++)
for (j = 0; j < 6; j++)
sum += array[i][j];
return sum;
}
static int
myfun2(int lines,
const int (*ap)[6])
{
int sum = 0;
int i, j;
#if 0
ap[0][0] = 1; /* read-only */
memset(ap, '\0', lines * 6 * sizeof(ap[0][0]));
#endif
for (i = 0; i < lines; i++)
for (j = 0; j < 6; j++)
sum += ap[i][j];
return sum;
}
static int
simplefun(const int array[6])
{
int sum = 0;
int i;
#if 0
array[0][0] = 1; /* read-only */
memset(array, '\0', lines * 6 * sizeof(array[0][0]));
#endif
for (i = 0; i < 6; i++)
sum += array[i];
return sum;
}
static int
simplefun2(const int ap[])
{
int sum = 0;
int i;
#if 0
ap[0][0] = 1; /* read-only */
memset(ap, '\0', lines * 6 * sizeof(ap[0][0]));
#endif
for (i = 0; i < 6; i++)
sum += ap[i];
return sum;
}
static int
callmyfun_bad(void)
{
int myarray[3][6] = {
{1,2,3,4,5,6},
{7,8,9,10,11,12},
{13,14,15,16,17,18}};
return myfun(sizeof(myarray)/sizeof(*myarray), myarray); /* incompatible pointer type */
}
static int
callmyfun_good(void)
{
int myarray[3][6] = {
{1,2,3,4,5,6},
{7,8,9,10,11,12},
{13,14,15,16,17,18}};
return myfun(sizeof(myarray)/sizeof(*myarray), (const int(*)[6]) myarray);
}
static int
callmyfun_good2(void)
{
int myarray[3][6] = {
{1,2,3,4,5,6},
{7,8,9,10,11,12},
{13,14,15,16,17,18}};
return myfun2(sizeof(myarray)/sizeof(*myarray), (const int(*)[6]) myarray);
}
static int
callsimplefun(void)
{
int myarray[6] = {1,2,3,4,5,6};
return simplefun(myarray);
}
static int
callsimplefun2(void)
{
int myarray[6] = {1,2,3,4,5,6};
return simplefun2(myarray);
}
int main(void);
int
main()
{
printf("bad = %d\n", callmyfun_bad());
printf("good = %d\n", callmyfun_good());
printf("good2 = %d\n", callmyfun_good2());
printf("simple = %d\n", callsimplefun());
printf("simple2 = %d\n", callsimplefun2());
exit(0);
}
/*
* Local Variables:
* eval: (make-local-variable 'compile-command)
* compile-command: (let ((fn (file-name-sans-extension (file-name-nondirectory (buffer-file-name))))) (concat "rm -f " fn "; " (default-value 'compile-command) " " fn " && ./" fn))
* End:
*/