We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,996 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Best way to define Structure

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 !

3
Contributors
4
Replies
1 Week
Discussion Span
2 Years Ago
Last Updated
5
Views
jay1648
Newbie Poster
4 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

predeclare the structure

typedef struct tagPerson Person;
struct tagPerson
{
  // blabla
};
Ancient Dragon
Achieved Level 70
Team Colleague
32,128 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69

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!!

jay1648
Newbie Poster
4 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

Ancient Dragon
Achieved Level 70
Team Colleague
32,128 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69

@jay1648

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

Hyperion
Light Poster
42 posts since Jun 2007
Reputation Points: 11
Solved Threads: 5
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0847 seconds using 2.77MB