Question about variable sized arrays

I'd like to represent strings "Pascal-style" with a length field
followed by the actual bytes. Reading the reference manual, I think
using a zero-length array should work in a struct like this:

      { i32, [0 x int8] }
However, I get a problem with referencing constants. The following
code doesn't compie:

    @foo = constant { i32, [3 x i8] } { i32 3, [3 x i8] c"foo" }

    define void @main( { i32, [0 x i8] }* %ptr) {
        store { i32, [3 x i8] }* @foo, { i32, [0 x i8] }** %ptr
llvm-as reports:

    Can't store '{ i32, [3 x i8] } *' into space of type '{ i32, [0 x i8] } *'

I'm wondering what the right way to do this is? I suppose I could
cast the pointer but is that really necessary? I'm pretty new to
LLVM so please forgive me if I'm missing something obvious ...

Thanks a lot!


You should bitcast the storage type of the constant (which includes the actual array bound) to the logical type (which includes the flexible array member instead). That is, bitcast { i32, [3 x i8] }* @foo to { i32, [0 x i8] }* before storing to %ptr.

This is somewhat analogous to C array type decay which allows you to pass "abc" (type const char[4]) to strlen(const char*). (However, in the case of array -> pointer conversions, a 'gep 0, 0' is sufficient. That technique doesn't work for flexible array constants.)