C flexible-arrays not imported

Could someone shine a bright light on this please?

// Flexible arrays in C

extern char * array;
extern char   another_array [];

extern unsigned long array_size;
// Access them in Swift

let p1 = array
    // array: UnsafeMutablePointer <CChar>!

let p2 = another_array
    // error: Cannot reference invalid declaration 'another_array'

let size = array_size
    // array_size: UInt

Why is there an error on the second statement?

Shouldn't the second statement be treated as the first one?

Swift imports C arrays as tuples, not pointers. If Swift imported arrays as pointers, then certain things would behave funnily (such as &another_array).

char *p and char p[] are different (except in the special case of function parameters). If you’re unfamiliar with array decay, it’s probably a good time to look it up. Swift does not decay C arrays to pointers.

If you control the header this is coming from, you can add an inline function returning char * that just returns another_array and you’ll be able to call it from Swift.

1 Like

Got it, thank you. :slight_smile:

1 Like