Liste des Groupes | Revenir à cl c |
On 08/04/2025 18:28, bart wrote:On 08/04/2025 15:50, David Brown wrote:
This doesn't make sense at all.Yes.The two types are entirely compatible.>
>
Are they?
So why does gcc report an error here (the two types are from my example):That is a different situation. In the first case, the types were defined in different translation units - and are compatible. In the second case, they are within the same translation unit, and are not compatible.
>
typedef struct point {float a; float b;} Point;
>
typedef float length;
typedef struct _tag {length x, y;} vector;
>
Point p;
vector v;
>
p=v;
>
c.c:14:5: error: incompatible types when assigning to type 'Point' {aka 'struct point'} from type
ector' {aka 'struct _tag'}
14 | p=v;
| ^
>
In C, if you declare two structs in the same translation unit with the same field types and the same field names, they are still different types. That is important for making new types - it is how you get strong typing in C (albeit with less user convenience than in some other languages). It means that you can't mix up two types just because of coincidences in the fields.So, yes, you are saying that. In short, if a programmer says that incompatible types T and U are the same (where the compiler can only see T in module A and U in module B), then the programmer must be trusted, even though they would be wrong.
This means that when you have a struct declaration in two translation units (locally in the C file, or via a header file - there is no distinction), they form two different types as they are separate declarations. If C did not specify that these were compatible, it would be impossible (without UB) to use struct or union types across translation units.
You are still wrong.Never mind. I've just spotted this at the end. In that case, and given what you said above where you arguing that Black is White, discussion is futile.
Les messages affichés proviennent d'usenet.