I’m trying to determine if two structs are layout-compatible. In my case, it’s not clear to me if pointers of different types are considered compatible. In the following example, are structs t1
and t2
layout-compatible?
typedef struct string {
size_t len;
char val[]; // flexible array member
} string;
struct t1 {
const char *name;
};
struct t2 {
string *name;
};
Since they are both pointers (and not pointers to functions), they have the same size and alignment, which is a prerequisite. But I do not understand all the other rules well enough to know if these are layout compatible. Knowing if they are layout-compatible factors into things like the common initial sequence for unions, casting pointers to compatible types, etc.