1 /* $NetBSD: expr_sizeof.c,v 1.4 2023/01/16 00:37:59 rillig Exp $ */ 2 # 3 "expr_sizeof.c" 3 4 /* 5 * C99 6.5.3.4 "The sizeof operator" 6 * C11 6.5.3.4 "The sizeof operator" 7 */ 8 9 /* 10 * A sizeof expression can either take a type name or an expression. 11 */ 12 void sink(unsigned long); 13 14 struct { 15 int member; 16 } s, *ps; 17 18 /* 19 * In a sizeof expression taking a type name, the type name must be enclosed 20 * in parentheses. 21 */ 22 /* expect+1: error: negative array dimension (-4) [20] */ 23 typedef int sizeof_int[-(int)sizeof(int)]; 24 25 /* 26 * In a sizeof expression taking an expression, the expression may or may not 27 * be enclosed in parentheses, like any other expression. 28 */ 29 /* expect+1: error: negative array dimension (-4) [20] */ 30 typedef int sizeof_paren_zero[-(int)sizeof(0)]; 31 /* expect+1: error: negative array dimension (-4) [20] */ 32 typedef int sizeof_zero[-(int)sizeof 0]; 33 34 /* 35 * Even though 's' is not a constant expression, 'sizeof s' is. 36 */ 37 /* expect+1: error: negative array dimension (-4) [20] */ 38 typedef int sizeof_global_var[-(int)sizeof s]; 39 /* expect+1: error: negative array dimension (-4) [20] */ 40 typedef int sizeof_paren_global_var[-(int)sizeof(s)]; 41 42 /* 43 * Even though 'sizeof(s)' may look like a function call expression, the 44 * parentheses around 's' are ordinary parentheses and do not influence the 45 * precedence. 46 * 47 * Therefore, the '.' following the '(s)' takes precedence over the 'sizeof'. 48 * Same for the '->' following the '(ps)'. Same for the '[0]' following the 49 * '(arr)'. 50 */ 51 /* expect+1: error: negative array dimension (-4) [20] */ 52 typedef int sizeof_paren_global_struct_member[-(int)sizeof(s).member]; 53 /* expect+1: error: negative array dimension (-4) [20] */ 54 typedef int sizeof_paren_global_ptr_struct_member[-(int)sizeof(ps)->member]; 55 int arr[] = { 1, 2, 3 }; 56 /* expect+1: error: negative array dimension (-3) [20] */ 57 typedef int arr_count[-(int)sizeof(arr) / (int)sizeof(arr)[0]]; 58 59 /* FIXME: 'n' is actually used, for the variable length array. */ 60 /* expect+2: warning: argument 'n' unused in function 'variable_length_array' [231] */ 61 void 62 variable_length_array(int n) 63 { 64 int local_arr[n + 5]; 65 66 /* 67 * Since the array length is not constant, it cannot be used in a 68 * typedef. Code like this is already rejected by the compiler. For 69 * simplicity, lint assumes that the array has length 1. 70 */ 71 /* expect+1: error: negative array dimension (-4) [20] */ 72 typedef int sizeof_local_arr[-(int)sizeof(local_arr)]; 73 } 74