Difficulty: Medium
Correct Answer: Yes, C allows a typedef for a pointer to an incomplete struct type, and the struct body can safely use that typedef for pointer members.
Explanation:
Introduction / Context:
Linked data structures in C often require a struct to contain a pointer to its own type, such as in linked lists or trees. The language supports this by allowing pointers to incomplete struct types. This question checks whether you understand how typedef interacts with forward declarations and incomplete types, and whether a typedef name for a pointer can be introduced before the full struct definition and then used inside that definition.
Given Data / Assumptions:
Concept / Approach:
C allows the use of a struct tag as soon as it has been introduced, even if the complete definition comes later. This is called an incomplete type. You cannot take sizeof an incomplete type or define variables of that exact type, but you can declare pointers to it. A typedef that names a pointer to an incomplete struct is therefore legal. Once the full struct definition appears, the typedef name ptr refers to a pointer to that now complete struct type, and the member declaration ptr next; is valid and commonly used in linked list definitions.
Step-by-Step Solution:
Step 1: Notice that struct employee is introduced in the typedef line, which forward declares the struct tag.
Step 2: Understand that at this point struct employee is incomplete but can be the target of a pointer type.
Step 3: The typedef struct employee *ptr; therefore defines ptr as "pointer to struct employee" and is valid.
Step 4: In the subsequent full definition of struct employee, the member next is declared with type ptr, which is a pointer to struct employee.
Step 5: Because C allows self referential structs through pointers, this pattern is both valid and idiomatic.
Verification / Alternative check:
Many textbooks show self referential structures using exactly this style: typedef struct node *NodePtr; struct node { int data; NodePtr next; }; The compiler accepts this because only pointers to the incomplete type are introduced before the body. If you attempt to declare a struct employee variable before the full definition, the compiler will reject sizeof and object layout, which confirms the distinction between using the tag for pointers and for complete objects.
Why Other Options Are Wrong:
Option B claims that the typedef must follow the full definition, which is not required by the C standard and would make many common idioms impossible. Option C suggests that this is only valid in C plus plus, but the pattern is standard C as well. Option D incorrectly states that a struct may not contain a pointer to its own type, even though this is exactly how linked lists and trees are typically defined in C.
Common Pitfalls:
A frequent misunderstanding is to think that any use of a struct tag before its full definition is illegal. The real rule is that incomplete types may appear in pointer declarations but not as complete object types requiring a known size. Another pitfall is confusing typedef names with new distinct types; here ptr is simply an alias for a pointer type and obeys the same rules as struct employee * in all contexts.
Final Answer:
Yes, it is valid in C to create a typedef for a pointer to an incomplete struct type and then use that typedef inside the struct definition for self referential pointer members.
Discussion & Comments