In the first compilation unit:
/* From .h file */
struct my_struct {
int foo;
long bar;
};
void my_struct_init(struct my_struct*);
int my_struct_get_int(struct my_struct*);
long my_struct_get_long(struct my_struct*);
/* From .c file */
void my_struct_init(struct my_struct* o) {
o->foo = 0;
o->bar = 0L;
}
int my_struct_get_int(struct my_struct* o) {
return o->foo;
}
long my_struct_get_long(struct my_struct* o) {
return o->bar;
}
In the second compilation unit:
/* From .h file */
struct my_struct {
/* Named differently */
int foo_different;
long bar_different;
};
void my_struct_init(struct my_struct*);
int my_struct_get_int(struct my_struct*);
long my_struct_get_long(struct my_struct*);
/* From .c file */
int main(void) {
struct my_struct o;
my_struct_init(&o);
if (o.foo_different != 0 || o.bar_different != 0L) {
/* (Just assume EXIT_FAILURE is 1) */
return 1;
}
o.foo_different = 1;
o.bar_different = 2L;
if (my_struct_get_int(&o) != 1 || my_struct_get_long(&o) != 2L) {
return 1;
}
return 0;
}
Although the two structs have the same name and the members have the same types and order, the members have different names.
Does that mean that the above is not defined? Or will this always succeed?
(For context, I am using this to prefix members with "_my_type_private_" if a header file is included normally, but kept the same if it is included from an internal implementation file, so that the type is not incomplete but it is harder to modify the struct accidentally)
EDIT: the question is not a duplicate of that question but it does provide the correct answer:
Moreover, two structure, union, or enumerated types declared in separate translation units are compatible if [for each pair of member declarations] one member of the pair is declared with a name, the other is declared with the same name
So they are not compatible types.