This definition
typedef struct{ // 1
char* phrase;
struct phrase_struct* next; // 2
}phrase_struct; // 3
defines 3 things:
- An anonymous structure
- A member which is a pointer to a forward-declared structure named
struct phrase_struct
- A
typedef
for the anonymous structure calledphrase_struct
Whenever you refer to a plain phrase_struct
, it refers to the typedef
. Whenever you refer to struct typedef
, it refers to the structure named phrase_struct
within the struct
tag namespace; this structure has been forward-declared (in (2)) and is an incomplete type, but it hasn't actually been fully defined yet.
The error message is unfortunately confusing. What the error is trying to say is this:
'=' : incompatible types - from 'struct phrase_struct *' to the typedef
'phrase_struct *'
because the undefined, forward-declared structure has no relation to the typedef
which has been defined.
The fix, as you found, is straightforward: instead of defining an anonymous structure, give the structure a name, and refer to that same name (in the struct
tag namespace) in the member definition:
typedef struct phrase_struct{ // 1
char* phrase;
struct phrase_struct* next; // 2
}phrase_struct; // 3
This defines:
- A structure named
struct phrase_struct
in thestruct
tag namespace - A member with a type of pointer to a
struct phrase_struct
instance (i.e. the type defined in (1)) - A
typedef
namedphrase_struct
, defined as an alias forstruct phrase_struct
It's confusing because the tag namespaces (struct
, union
, and enum
) are separate from the ordinary typedef
namespace.
See also Difference between struct
and typedef struct
in C++? and What is a typedef enum
in Objective-C?.