Archived

This topic is now archived and is closed to further replies.

mertz

c programing: using "struct" and "typedef struct"

Recommended Posts

mertz    122
what is the purpose of defining some structures like: struct tree_s { float height; int branches; }; and some as: typedef struct { int a; int b; } pair_t; why is this _t, _s notation, and their coresponding declairations, so redily used? Btw I know that one requres that I create instances beginning with the struct tag, and that typedef allows me to just type pair_t pairOne; So thats not what im after. What im after is more along the lines of the programing paradigm behind the _t _s notation. Thanks in advance. Brett [edited by - mertz on October 2, 2002 9:21:04 AM]

Share this post


Link to post
Share on other sites
Zyroth    122
If I remember right, the first one isn't allow in straight C but was implemented in C++. They both work.

_t is for types that you've defined yourself using typedef, while _s is for stucts.

[edited by - zyroth on October 2, 2002 9:52:53 AM]

Share this post


Link to post
Share on other sites
LessBread    1415
The first instance is allowed in straight C - it just means that every time a variable of such a structure is declared the declaration must use the ''struct'' keyword. I think the OP gets that part of it.


The _t and _s appendages are not part of the language itself. Those are just elements of coding style used to indicate whether the structure has been typedefed or not.

I generally typedef all structures as it just makes things easier. However, I use a more Microsoft style of approach

typedef struct tagMyStruct {
// members
}
MYSTRUCT, *PMYSTRUCT;

MYSTRUCT AVariableOfTypeMyStruct;
MYSTRUCT *pAVariableOfTypeMyStruct;
PMYSTRUCT *ppAVariableOfTypeMyStruct; // this would be a pointer pointer




Share this post


Link to post
Share on other sites
mertz    122
Tanks guys but thats not exactly what I was after, and you are wrong in some cases: Both work in pure C. Yes I understand the language-based reason for _t _s but the reason for using _s instaid of _t all the time still has not been adressed in the thread. What would professional game programers like John Carmack use the _s _t notation? There must be a programing paradigm behind it. As far as I can see there is no good reason for using _s instaid of _t declarations of a struct templates:

typedef struct {

int fast;
int easier;

} quick;

quick quickObject;

struct slow {

int more_typing;
int tiresom;

};

struct slow slowObject;

John Carmack, Chris Hargrove, many others, they all use this _t _s shit. WHY????? (they are very intelligent, so I guess it denotes something, rather than indicating that they are brain dead.)




[edited by - mertz on October 2, 2002 9:41:07 PM]

Share this post


Link to post
Share on other sites
mertz    122
And another thing! (im really getting frusrtated by this )

This is taken streight from the Sams 21 day guide to C

"You can use the typedef keyword to create a synonym for a structure or union type. For example, the following statements define coord as a synonym for the indicated structure:


typedef struct {
int x;
int y;
} coord;

You can then declare instances of this structure using the coord identifier:


coord topleft, bottomright;

Note that a typedef is different from a structure tag, as described earlier in this chapter. If you write


struct coord {
int x;
int y;
};

the identifier coord is a tag for the structure. You can use the tag to declare instances of the structure, but unlike with a typedef, you must include the struct keyword:


struct coord topleft, bottomright;

Whether you use typedef or a structure tag to declare structures makes little difference. (*)Using typedef results in slightly more concise code(*), because the struct keyword doesn't need to be used. On the other hand, using a tag and having the struct keyword explicit makes it clear that it is a structure being declared."

So in others words, my question is, why do top prorgammers append some variables with _s and make it explict that its a struct varibale.

Another thing:, below is some code from the QuakeIII SDK. here the typedef struct keyphrase is used, but there is a tag there too, and the synonym below. Please explain?

typedef struct bot_input_s
{
float thinktime;
vec3_t dir;
float speed;
vec3_t viewangles;
int actionflags;
int weapon;
} bot_input_t;

Thanks again for replies.

PS: only answer if you know, I have my theories on this too but I would like an informed explaination.

Brett.



[edited by - mertz on October 2, 2002 9:43:35 PM]

Share this post


Link to post
Share on other sites
dMDI    122
Why must there be a paradigm behind it? It is just a notation programmers use to aid them to distinguish between typdefed and non typedefed structs.

There is nothing "special" behind it.

Share this post


Link to post
Share on other sites
LessBread    1415
As dMDI says - the _s _t notations are stylistic conventions those coders use. Other coders use different conventions. Since it is a style issue and not a language issue - only those coders themselves ''know'' their reasons. The best the rest of us can do is guess - or ask them.

In this example

typedef struct bot_input_s {...} bot_input_t;

bot_input_t is a lot easier to use to declare a variable. It also makes mallocating memory for a pointer to that type a lot easier too

bot_input_t *bot = malloc(sizeof(bot_input_t));

is a lot more succinct than

struct bot_input_s *bot = malloc(sizeof(struct bot_input_s));

especially in situations where the size_t argument to malloc is an equation.

As for why bot_input_s is needed - perhaps that makes it easier for the debugger to latch on to the structure definition.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Yes what youre saying is quite accurate and true, however, this notation is widespread in C code; it is equivalent to the "C" prefix for a class (CClassName). It is not very exclusive al all. My observation is that some structures have a "tag_s", and others dont, having only a "synonym_t". So this notation, albeit not a language feature, appears in many C based game projects. To anyone who might know: what are the possible meanings/uses for A) defining a struct without a "tag_s":

typedef struct {
// members...
} structure_t;

B) with a "tag_s":

typedef struct structure_s {
// members...
} structure_t;

I know I sound like an ignorant beginner, but I figure there is some kind of general meaning behind the differing structure definitions.

Thanks.

Share this post


Link to post
Share on other sites
Darrell L    122
Besides just coding practices, having a tag for your struct is useful when you need to access a field/member through the struct definition itself, not through the declared variables of that type.

I can''t think of an example off-hand, but I seem to remember using it with sizeof sometimes. sizeof(struct_s.field1) got me what I needed, whereas sizeof(var.field1) got me something different. Can''t remember what data type field1 was now though. Too early.

Share this post


Link to post
Share on other sites
dMDI    122
Perhaps they forgot to append the _s ?
Why are you so interested anyway, it would be much more advantageous to actually learn more programming skills and become one of the style trend setters, than to bother about how other programmers are naming their structs.

Share this post


Link to post
Share on other sites
nicho_tedja    122
I think it''s just a matter of programming style. Programmers have their own styles in naming classes, structs, vars, functions, etc. Hungarian Notation is one of them and used by Windows. _s and _t is probably just another style. If I were you, I''d just pick my own comfortable style and ignore those _s and _t. They are just names anyway.

One reason I can see is so that the type of one identifier can be distinguished from others. If you have:

coord x1;

What is it? a variable? a struct? a class? In order to know it, you have to look at the definition, and if your code is 100,000 lines with 100 files long, [:D] good luck. Without the help of a good IDE, that takes years.


My compiler generates one error message: "does not compile."

Share this post


Link to post
Share on other sites
mertz    122
dMDI what you are saying i hear, but it it also adventageous to echo the strokes of the master painters, before you in turn learn mastery and make your own marsterpiece. I think that learning from someone who has mastery is very important. You dont learn in a void, and you cannot learn everything from theory alone, which is what a book, or tutorial is. At some stage you need to look at those who went before you, before you progress forwards. So in short: id like to understand the coding pracrtices of Carmack, and Hargrove, and others, so that I can comprehend them, and from that comprehension grow my own problem-solving skills.

Share this post


Link to post
Share on other sites