source: doc/theses/mike_brooks_MMath/programs/bkgd-carray-arrty.c @ 72713e5

Last change on this file since 72713e5 was d3a49864, checked in by Peter A. Buhr <pabuhr@…>, 8 weeks ago

work on Figure 2.1

  • Property mode set to 100644
File size: 4.0 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 ar[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(ar) == 40 );              $\C{// array}$
38        static_assert( sizeof(&ar) == 8 );              $\C{// pointer to array}$
39        static_assert( sizeof(ar[0]) == 4 );    $\C{// first element}$
40        static_assert( sizeof(&(ar[0])) == 8 ); $\C{// pointer to first element}$
41
42        typeof(&ar) x = &ar;                                    $\C{// x is pointer to array}$
43        typeof(&(ar[0])) y = &ar[0];                    $\C{// y is pointer to first element}$
44        @x = y;@                                                                $\C{// ill-typed}$
45        @y = x;@                                                                $\C{// ill-typed}$
46        static_assert( sizeof(typeof(ar)) == 40 ); $\C{// array}$
47        static_assert( sizeof(typeof(&ar)) == 8 );      $\C{// pointer to array}$
48        static_assert( sizeof(typeof(ar[0])) == 4 ); $\C{// first element}$
49        static_assert( sizeof(typeof(&(ar[0]))) == 8 ); $\C{// pointer to first element}$
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( &ar );
58
59        float fs[] = {3.14, 1.77};
60        char cs[] = "hello";
61        static_assert( sizeof(fs) == 2 * sizeof(float) );
62        static_assert( sizeof(cs) == 6 * sizeof(char) );  $\C{// 5 letters + 1 null terminator}$
63
64        float fm[][2] = { {3.14, 1.77}, {12.4, 0.01}, {7.8, 1.23} };  $\C{// brackets define structuring}$
65        char cm[][sizeof("hello")] = { "hello", "hello", "hello" };
66        static_assert( sizeof(fm) == 3 * 2 * sizeof(float) );
67        static_assert( sizeof(cm) == 3 * 6 * sizeof(char) );
68}
69
70void syntaxReferenceCheck(void) {
71        // $\rightarrow$ & (base element)
72        //     & @float@
73        //     & @float x;@
74        //     & @[ float ]@
75        //     & @[ float ]@
76        float x0;
77
78        // $\rightarrow$ & pointer
79        //     & @float *@
80        //     & @float * x;@
81        //     & @[ * float ]@
82        //     & @[ * float ]@
83        float * x1;
84
85        // $\rightarrow$ & array
86        //     & @float[10]@
87        //     & @float x[10];@
88        //     & @[ [10] float ]@
89        //     & @[ array(float, 10) ]@
90        float x2[10];
91
92        typeof(float[10]) x2b;
93
94        // & array of pointers
95        //     & @(float*)[10]@
96        //     & @float *x[10];@
97        //     & @[ [10] * float ]@
98        //     & @[ array(*float, 10) ]@
99        float *x3[10];
100//    (float *)x3a[10];  NO
101
102        // $\rightarrow$ & pointer to array
103        //     & @float(*)[10]@
104        //     & @float (*x)[10];@
105        //     & @[ * [10] float ]@
106        //     & @[ * array(float, 10) ]@
107        float (*x4)[10];
108
109        // & pointer to array
110        //     & @(float*)(*)[10]@
111        //     & @float *(*x)[10];@
112        //     & @[ * [10] * float ]@
113        //     & @[ * array(*float, 10) ]@
114        float *(*x5)[10];
115        x5 =     (float*(*)[10]) x4;
116//    x5 =     (float(*)[10]) x4;  // wrong target type; meta test suggesting above cast uses correct type
117
118        // [here]
119        // const
120
121        // [later]
122        // static
123        // star as dimension
124        // under pointer decay:                int p1[const 3]  being  int const *p1
125
126        const float * y1;
127        float const * y2;
128        float * const y3;
129
130        y1 = 0;
131        y2 = 0;
132        // y3 = 0; // bad
133
134        // *y1 = 3.14; // bad
135        // *y2 = 3.14; // bad
136        *y3 = 3.14;
137
138        const float z1 = 1.414;
139        float const z2 = 1.414;
140
141        // z1 = 3.14; // bad
142        // z2 = 3.14; // bad
143
144
145}
146
147#define T float
148void stx2() { const T x[10];
149//            x[5] = 3.14; // bad
150                        }
151void stx3() { T const x[10];
152//            x[5] = 3.14; // bad
153                        }
154
155// Local Variables: //
156// compile-command: "sed -f sedcmd bkgd-carray-arrty.c | gcc -x c -" //
157// End: //
Note: See TracBrowser for help on using the repository browser.