I'm passing an array of structs from Swift to a C function. The struct looks like this:
struct Struct {
int a;
float b;
float c;
const char* d;
const char* e;
const char* f;
const char* g;
int h[4];
};
Function signature of the C function:
void test(struct Struct* structs);
Weirdly, when I print d in the C function, it's often something different than what I set it to in the Swift code: usually an empty string or some garbage. When I set d to a very long string, it works correctly. The other strings are passed correctly too. Is that some struct alignment issue?
sizeofandalignofofintandfloatin C? What are the actual pointer values? What compiler are you using, and for what machine?char *members point to C strings which were temporarily created from Swift strings, and are already deallocated or overwritten when the C function is called.Struct("test"). But it seems that your guess was correct, the C strings are overwritten by whatever is written to the stack...