Hi,

Please suggest me all possible ways of declaring structures in my C codes. I want to be able to create a structure which I can use in my methods without have to use the keyword "struct" each time to declare it. One way I already know of is :

typedef struct
{
	char firstName[50];
	char lastName[50];
}Person;

Creating it this way, I can use it in other methods like this (which is what I want, without using the struct keyword to declare) :

Person person1;

However, the problem I'm facing with this is that I can't get it to work when I want to create a member variable in the struct which is of it's own struct type. This is what I'm trying :

typedef struct
{
	char firstName[50];
	char lastName[50];
	Person firstPerson;
	Person secondPerson;
}Person;

And, obviously this doesn't compile. Would anybody please have a suggestion on how to accomplish this ?

As my question says, I'm still new to C. Please help !

Dani AI

Generated

Good answers already appear in the thread, so here’s a concise, practical supplement that fills a few gaps and clears up the common confusions.

The core rule: a struct cannot directly contain a member that is the same complete type as itself because that would require infinite size. That is why self-referential members must be pointers (a pointer has a known, fixed size). ’s forward-declaration idea is the usual way to let the compiler know the tag exists before you use a pointer to it. was on the right track about pointers but ran into the common mix-up between the struct tag and a typedef alias.

Clarify the tag vs typedef point: the struct tag (the identifier that follows the keyword struct) and the typedef name (an alias you create) are separate names in C. The typedef alias is not available until the typedef statement completes, so inside the struct body you either use the struct tag form or you ensure a typedef is already visible. That explains why swapping names or trying to use the alias too early causes compiler errors.

Practical tips and pitfalls: put the forward declaration and any typedefs in headers in the correct order so users see the alias before using it; prefer explicit names to avoid confusion (don’t reuse the same identifier for tag and typedef if it confuses your team); remember ownership and lifetime when you use pointers — allocate and free consistently or use clear owner semantics; for mutually referencing types (A refers to B and B refers to A) use forward declarations for both. Note: C++ treats struct names as type names differently, so code that compiles in C++ may still need explicit struct/typedef handling in C.

These points should make the existing answers clearer and help avoid the typical compilation and maintenance traps.

Recommended Answers

All 4 Replies

predeclare the structure

typedef struct tagPerson Person;
struct tagPerson
{
  // blabla
};

Wow !
This works.
Thanks, Ancient Dragon !

Can somebody please explain me though, what is "tagPerson" and "Person" in this case ? As I understand, "tagPerson" is the structure which we defined with it's member variables. But I'm confused about "Person" in this case. Is this a new "type" that is derived from "tagPerson" ? Or is this a variable of type "tagPerson" ? Someone please clarify!!

Person is the same thing that you originally posted. Its just another name for struct tagPerson Person is not an instance of the structure, but just a typedef for it.

typedef struct tagPerson
{
	char firstName[50];
	char lastName[50];
	Person firstPerson;
	Person secondPerson;
}Person;

won't compile because Person struct cannot have a variable of type Person in it, rather it should be a pointer to Person like:

typedef struct tagPerson
{
	char firstName[50];
	char lastName[50];
	tagPerson* firstPerson;
	tagPerson* secondPerson;
}Person;

This code piece combines snippet suggested by Ancient Dragon in one :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.