source: doc/theses/mike_brooks_MMath/programs/bkgd-carray-arrty.c@ 30aab55

Last change on this file since 30aab55 was a885357, checked in by Peter A. Buhr <pabuhr@…>, 19 months ago

update compile commands

  • Property mode set to 100644
File size: 3.6 KB
Line 
1#include <stdio.h>
2#include <assert.h>
3#include <stdlib.h>
4
5#define SHOW(x, fmt) printf( #x ": " fmt "\n", x )
6
7#ifdef ERRS
8#define ERR(...) __VA_ARGS__
9#else
10#define ERR(...)
11#endif
12
13// int main( int argc, const char *argv[] ) {
14// assert(argc == 2);
15// const int n = atoi(argv[1]);
16// assert(0 < n && n < 1000);
17
18// float a1[42];
19// float a2[n];
20// SHOW(sizeof(a1), "%zd");
21// SHOW(sizeof(a2), "%zd");
22
23// }
24
25
26 // SHOW(sizeof( a ), "%zd");
27 // SHOW(sizeof(&a ), "%zd");
28 // SHOW(sizeof( a[0]), "%zd");
29 // SHOW(sizeof(&a[0]), "%zd");
30
31
32
33int main() {
34 float a[10];
35 static_assert(sizeof(float) == 4); $\C{// floats (array elements) are 4 bytes}$
36 static_assert(sizeof(void*) == 8); $\C{// pointers are 8 bytes}$
37 static_assert(sizeof(a) == 40); $\C{// array}$
38 static_assert(sizeof(&a) == 8 ); $\C{// pointer to array}$
39 static_assert(sizeof(a[0]) == 4 ); $\C{// first element}$
40 static_assert(sizeof(&(a[0])) == 8 ); $\C{// pointer to first element}$
41
42 typeof(&a) x; $\C{// x is pointer to array}$
43 typeof(&(a[0])) y; $\C{// y is pointer to first element}$
44 @x = y;@ $\C{// ill-typed}$
45 @y = x;@ $\C{// ill-typed}$
46 static_assert(sizeof(typeof(a)) == 40);
47 static_assert(sizeof(typeof(&a)) == 8 );
48 static_assert(sizeof(typeof(a[0])) == 4 );
49 static_assert(sizeof(typeof(&(a[0]))) == 8 );
50
51 void f( float (*pa)[10] ) {
52 static_assert(sizeof( *pa ) == 40); $\C{// array}$
53 static_assert(sizeof( pa ) == 8 ); $\C{// pointer to array}$
54 static_assert(sizeof( (*pa)[0] ) == 4 ); $\C{// first element}$
55 static_assert(sizeof(&((*pa)[0])) == 8 ); $\C{// pointer to first element}$
56 }
57 f( & a );
58
59 float fs[] = {3.14, 1.707};
60 char cs[] = "hello";
61
62 static_assert( sizeof(fs) == 2 * sizeof(float) );
63 static_assert( sizeof(cs) == 6 * sizeof(char) ); $\C{// 5 letters + 1 null terminator}$
64}
65
66void syntaxReferenceCheck(void) {
67 // $\rightarrow$ & (base element)
68 // & @float@
69 // & @float x;@
70 // & @[ float ]@
71 // & @[ float ]@
72 float x0;
73
74 // $\rightarrow$ & pointer
75 // & @float *@
76 // & @float * x;@
77 // & @[ * float ]@
78 // & @[ * float ]@
79 float * x1;
80
81 // $\rightarrow$ & array
82 // & @float[10]@
83 // & @float x[10];@
84 // & @[ [10] float ]@
85 // & @[ array(float, 10) ]@
86 float x2[10];
87
88 typeof(float[10]) x2b;
89
90 // & array of pointers
91 // & @(float*)[10]@
92 // & @float *x[10];@
93 // & @[ [10] * float ]@
94 // & @[ array(*float, 10) ]@
95 float *x3[10];
96// (float *)x3a[10]; NO
97
98 // $\rightarrow$ & pointer to array
99 // & @float(*)[10]@
100 // & @float (*x)[10];@
101 // & @[ * [10] float ]@
102 // & @[ * array(float, 10) ]@
103 float (*x4)[10];
104
105 // & pointer to array
106 // & @(float*)(*)[10]@
107 // & @float *(*x)[10];@
108 // & @[ * [10] * float ]@
109 // & @[ * array(*float, 10) ]@
110 float *(*x5)[10];
111 x5 = (float*(*)[10]) x4;
112// x5 = (float(*)[10]) x4; // wrong target type; meta test suggesting above cast uses correct type
113
114 // [here]
115 // const
116
117 // [later]
118 // static
119 // star as dimension
120 // under pointer decay: int p1[const 3] being int const *p1
121
122 const float * y1;
123 float const * y2;
124 float * const y3;
125
126 y1 = 0;
127 y2 = 0;
128 // y3 = 0; // bad
129
130 // *y1 = 3.14; // bad
131 // *y2 = 3.14; // bad
132 *y3 = 3.14;
133
134 const float z1 = 1.414;
135 float const z2 = 1.414;
136
137 // z1 = 3.14; // bad
138 // z2 = 3.14; // bad
139
140
141}
142
143#define T float
144void stx2() { const T x[10];
145// x[5] = 3.14; // bad
146 }
147void stx3() { T const x[10];
148// x[5] = 3.14; // bad
149 }
150
151// Local Variables: //
152// compile-command: "sed -f sedcmd bkgd-carray-arrty.c | gcc -x c -" //
153// End: //
Note: See TracBrowser for help on using the repository browser.