Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

typedef 1

Status
Not open for further replies.

tonyhur

Programmer
Feb 11, 2010
1
CH
what does

typedef struct _GRBmodel GRBmodel;

exactly mean? Is "GRBmodel" just another name for "struct _GRBmodel"?
But then what does "struct _GRBmodel" mean? is it simply a pointer?

Thanks for help
Tony
 
That is a little bit of an odd syntax.

Normally one would code something like:

typedef struct _GRBmodel {
....
whatever is in the struct
....
} GRBmodel;

This would allow you to refer to the model as GRBmodel or as _GRBmodel.

It looks to me like your statement is just a long form of redefinition to allow you to use the name GRBmodel.
 
You can use either

struct _GRBmodel xxx;

or

GRBmodel xxx;

They mean the same thing. This method of programming has crept in because of recursive data structures. If it is not recursive, you can just use

typedef struct
{
...
} GRBModel;

The problem comes when it is recursive: how do you use something before it is defined since it cannot be forward declared? You will get a compilation error if you have something like

typedef struct
{
...
Recursive* left;
Recursive* right;
} Recursive;

To get around it, the struct needs to be given a name

typedef struct _Recursive
{
...
struct _Recursive* left;
struct _Recursive* right;
} Recursive;

Since it is not consistent: some have names and some don't have names, some developers have decided that everything should have names regardless of whether they are of any use.

The other thing you can do with typedef is multiple definitions.

typedef _Recursive Recursive, *PRecursive, **PPRecursive;

This will be familiar stuff for MS programmers: it appears all over the windows header files.
 
Sorry, I went off on a tangent. The original question, what does struct _GRBModel mean - it is the structure which should have been declared previously.
 
I wouldn't say you went off on a tangent. Your post was quite informative. I have been programming with C for a good 20 years, professionally for 15 and learned something new.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top