Is there a structural type equivalence in C?
3 に答える
Strictly, no - differently named types are different types, even if the structure of the types is the same. (Of course, a typedef
just introduces an alternative name for an existing type; such types are the same type.)
However, in practice, there are a number of stunts you can pull and get away with. But strictly, they are cheating. Using void pointers is one way of subverting the system; another is not using prototype declarations of functions; variable length argument lists can be another.
2つを比較して同等かどうかを尋ねている場合struct
、答えはノーです。代入のみがあり、関数から構造体を返すことができます。
C には 2 つの構造体を比較する演算子はありません。代わりに memcmp を使用できます。
if( memcmp( &structvar1, &structvar2, sizeof structvar1 ) )
puts("not equal");
else
puts("equal");