c programing: using "struct" and "typedef struct"
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]
If it can be remediedWhy be unhappy about that?If it cannot be remediedWhat is the use of being unhappy about that?It's time to put a stop to this procrastination, later. Do, or do not; there is no try.
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]
_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]
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
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
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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]
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]
If it can be remediedWhy be unhappy about that?If it cannot be remediedWhat is the use of being unhappy about that?It's time to put a stop to this procrastination, later. Do, or do not; there is no try.
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]
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]
If it can be remediedWhy be unhappy about that?If it cannot be remediedWhat is the use of being unhappy about that?It's time to put a stop to this procrastination, later. Do, or do not; there is no try.
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.
There is nothing "special" behind it.
"I don''t know with what weapons the third world war will be fought, but I know the fourth will be fought with sticks and stones." Einstein
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.
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.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
October 03, 2002 04:52 AM
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.
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.
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.
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.
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.
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.
"I don''t know with what weapons the third world war will be fought, but I know the fourth will be fought with sticks and stones." Einstein
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement