Andrew Rice Andrew Rice - 2 months ago 8x
C Question

Why is this cast to a struct pointer not a compile-time constant?

The C program below fails to compile with gcc due to an error about the size of test_array not being a compile time constant. Why?

struct HWND__ { int unused; };
typedef struct HWND__ *HWND;

void test()
static int test_array[ (unsigned long long)((HWND)1) ];

Error produced is:

test.c: In function ‘test’:
test.c:5:14: error: storage size of ‘test_array’ isn’t constant
static int test_array[ (unsigned long long)((HWND)1) ];

ams ams

From the C11 standard, section 6.6:

An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, _Alignof expressions, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof or _Alignof operator.

An integer constant expression is required in a number of contexts such as the size of a bit-field member of a structure, the value of an enumeration constant, and the size of a non-variable length array.

I think that's why your code is invalid.

I suggest submitting a bug report to the hsc2hs developers, and fixing up these cases manually in the meantime.