| [7972603] | 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 |  | 
|---|
| [266732e] | 26 | // SHOW(sizeof( a   ), "%zd"); | 
|---|
|  | 27 | // SHOW(sizeof(&a   ), "%zd"); | 
|---|
|  | 28 | // SHOW(sizeof( a[0]), "%zd"); | 
|---|
|  | 29 | // SHOW(sizeof(&a[0]), "%zd"); | 
|---|
| [7972603] | 30 |  | 
|---|
|  | 31 |  | 
|---|
|  | 32 |  | 
|---|
|  | 33 | int main() { | 
|---|
| [b5bfb16] | 34 | float ar[10]; | 
|---|
|  | 35 | static_assert( sizeof(float) == 4 );    $\C{// floats (array elements) are 4 bytes}$ | 
|---|
| [df699e0] | 36 | static_assert( sizeof(void *) == 8 );   $\C{// pointers are 8 bytes}$ | 
|---|
| [b5bfb16] | 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}$ | 
|---|
| [266732e] | 44 | @x = y;@                                                                $\C{// ill-typed}$ | 
|---|
|  | 45 | @y = x;@                                                                $\C{// ill-typed}$ | 
|---|
| [b5bfb16] | 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}$ | 
|---|
| [266732e] | 50 |  | 
|---|
|  | 51 | void f( float (*pa)[10] ) { | 
|---|
| [b5bfb16] | 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}$ | 
|---|
| [266732e] | 56 | } | 
|---|
| [b5bfb16] | 57 | f( &ar ); | 
|---|
| [266732e] | 58 |  | 
|---|
| [d3a49864] | 59 | float fs[] = {3.14, 1.77}; | 
|---|
| [266732e] | 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}$ | 
|---|
| [b5bfb16] | 63 |  | 
|---|
| [d3a49864] | 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) ); | 
|---|
| [7972603] | 68 | } | 
|---|
|  | 69 |  | 
|---|
|  | 70 | void syntaxReferenceCheck(void) { | 
|---|
| [266732e] | 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]; | 
|---|
| [7972603] | 100 | //    (float *)x3a[10];  NO | 
|---|
|  | 101 |  | 
|---|
| [266732e] | 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; | 
|---|
| [7972603] | 116 | //    x5 =     (float(*)[10]) x4;  // wrong target type; meta test suggesting above cast uses correct type | 
|---|
|  | 117 |  | 
|---|
| [266732e] | 118 | // [here] | 
|---|
|  | 119 | // const | 
|---|
| [7972603] | 120 |  | 
|---|
| [266732e] | 121 | // [later] | 
|---|
|  | 122 | // static | 
|---|
|  | 123 | // star as dimension | 
|---|
|  | 124 | // under pointer decay:                int p1[const 3]  being  int const *p1 | 
|---|
| [7972603] | 125 |  | 
|---|
| [266732e] | 126 | const float * y1; | 
|---|
|  | 127 | float const * y2; | 
|---|
|  | 128 | float * const y3; | 
|---|
| [7972603] | 129 |  | 
|---|
| [266732e] | 130 | y1 = 0; | 
|---|
|  | 131 | y2 = 0; | 
|---|
|  | 132 | // y3 = 0; // bad | 
|---|
| [7972603] | 133 |  | 
|---|
| [266732e] | 134 | // *y1 = 3.14; // bad | 
|---|
|  | 135 | // *y2 = 3.14; // bad | 
|---|
|  | 136 | *y3 = 3.14; | 
|---|
| [7972603] | 137 |  | 
|---|
| [266732e] | 138 | const float z1 = 1.414; | 
|---|
|  | 139 | float const z2 = 1.414; | 
|---|
| [7972603] | 140 |  | 
|---|
| [266732e] | 141 | // z1 = 3.14; // bad | 
|---|
|  | 142 | // z2 = 3.14; // bad | 
|---|
| [7972603] | 143 |  | 
|---|
|  | 144 |  | 
|---|
|  | 145 | } | 
|---|
|  | 146 |  | 
|---|
|  | 147 | #define T float | 
|---|
|  | 148 | void stx2() { const T x[10]; | 
|---|
|  | 149 | //            x[5] = 3.14; // bad | 
|---|
| [e26a842] | 150 | } | 
|---|
| [7972603] | 151 | void stx3() { T const x[10]; | 
|---|
|  | 152 | //            x[5] = 3.14; // bad | 
|---|
| [e26a842] | 153 | } | 
|---|
| [266732e] | 154 |  | 
|---|
|  | 155 | // Local Variables: // | 
|---|
| [df699e0] | 156 | // compile-command: "sed -f sedcmd bkgd-carray-arrty.c | gcc-11 -Wall -Wextra -x c -" // | 
|---|
| [266732e] | 157 | // End: // | 
|---|