Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compatible types and structures in C

Tags:

I have the following code:

int main(void) {     struct { int x; } a, b;     struct { int x; } c;     struct { int x; } *p;      b = a;   /* OK */     c = a;   /* Doesn't work */     p = &a;  /* Doesn't work */      return 0; } 

which fails to compile under GCC (3.4.6), with the following error:

test.c:8: error: incompatible types in assignment test.c:9: warning: assignment from incompatible pointer type 

Now, from what I understand (admittedly from the C99 standard), is that a and c should be compatible types, as they fulfill all the criteria in section 6.2.7, paragraph 1. I've tried compiling with std=c99, to no avail.

Presumably my interpretation of the standard is wrong?

Addendum

Incidentally, this question arises because I wanted to declare some template-like macros to wrap various datatypes without the overhead of having to declare named types/typedefs everywhere, e.g. a trivial example:

#define LINKED_LIST(T)   \     struct {             \         T    *pHead;     \         T    *pTail;     \     }  ...  LINKED_LIST(foo) list1; LINKED_LIST(foo) list2;  ...  LINKED_LIST(foo) *pList = &list1;  /* Doesn't work */ 
like image 500
Oliver Charlesworth Avatar asked May 25 '10 12:05

Oliver Charlesworth


People also ask

What are compatible types in C?

In C, compatible types are defined as: two types that can be used together without modification (as in an assignment expression) two types that can be substituted one for the other without modification.

What are structure types in C?

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, float, char, etc.).

What are the two types of compatibility?

Romantic compatibility can also involve many aspects of your life. There's: physical compatibility. intellectual compatibility.

What is type compatibility explain in detail?

Compatibility between types refers to the similarity of two types to each other. Type compatibility is important during type conversions and operations. All valid declarations in the same scope that refer to the same object or function must have compatible types.


2 Answers

struct { int x; } is a anonymous structure tag, two anonymous structures cannot have "the same name", which is a necessary condition for type compatibility. You can declare types that are compatible with a non-anonymous structure using typedef.

struct tmp { int x; }; // declare structure tag typedef struct tmp type1; typedef struct tmp type2; // declare 3 types compatible with struct tmp typedef struct tmp type3; // and with each other  type1 a, b; type2 c; type3 *p; b = a; c = a; p = &a; 
like image 87
Giovanni Funchal Avatar answered Sep 25 '22 17:09

Giovanni Funchal


Looking at the draft specification I'm guessing you're relying on the conditions that come after the statement:

Moreover, two structure, union, or enumerated types declared in separate translation units are compatible if their tags and members satisfy the following requirements ...

I think that the fact that these are all decared in the same C file means that they are in a single translation unit.

At a guess it would seem that this guarantees that when two C files include a header that declares a type then instances of that type will be compatible.

like image 43
torak Avatar answered Sep 22 '22 17:09

torak