typedef is for defining something as a type. For instance:
typedef struct {
int a;
int b;
} THINGY;
...defines THINGY as the given struct. That way, you can use it like this:
THINGY t;
...rather than:
struct _THINGY_STRUCT {
int a;
int b;
};
struct _THINGY_STRUCT t;
...which is a bit more verbose. typedefs can make some things dramatically clearer, specially pointers to functions.
Answer from T.J. Crowder on Stack Overflownew to C. what does typedef mean in this case and why's it used?
Is it bad to use typedef?
How to properly use `typedef` for structs in C? - Stack Overflow
When is it appropriate to typedef?
Videos
typedef is for defining something as a type. For instance:
typedef struct {
int a;
int b;
} THINGY;
...defines THINGY as the given struct. That way, you can use it like this:
THINGY t;
...rather than:
struct _THINGY_STRUCT {
int a;
int b;
};
struct _THINGY_STRUCT t;
...which is a bit more verbose. typedefs can make some things dramatically clearer, specially pointers to functions.
From wikipedia:
typedef is a keyword in the C and C++ programming languages. The purpose of typedef is to assign alternative names to existing types, most often those whose standard declaration is cumbersome, potentially confusing, or likely to vary from one implementation to another.
And:
K&R states that there are two reasons for using a typedef. First, it provides a means to make a program more portable. Instead of having to change a type everywhere it appears throughout the program's source files, only a single typedef statement needs to be changed. Second, a typedef can make a complex declaration easier to understand.
And an argument against:
He (Greg K.H.) argues that this practice not only unnecessarily obfuscates code, it can also cause programmers to accidentally misuse large structures thinking them to be simple types.
typedef is for defining something as a type. For instance:
typedef struct {
int a;
int b;
} THINGY;
...defines THINGY as the given struct. That way, you can use it like this:
THINGY t;
...rather than:
struct _THINGY_STRUCT {
int a;
int b;
};
struct _THINGY_STRUCT t;
...which is a bit more verbose. typedefs can make some things dramatically clearer, specially pointers to functions.
Answer from T.J. Crowder on Stack Overflowtypedef enum { STATEMENT_INSERT, STATEMENT_SELECT } StatementType;
typedef struct {
StatementType type;
} Statement;example, in this case, what is the benefit? I know what enums are, but what does the struct do also?
When making various structs, unions, enums, etc. is it bad to use typedef? I was looking at someone elseโs code and for a struct they would always just specify that it was a struct whenever they referenced it in code instead of using typedef. I was wondering if there was a stylistic guideline or a set of rules on using typedef.
What this does:
typedef struct {
int a;
int b;
} ab_t;
Is define an anonymous struct and give it the alias ab_t. For this case there's no problem as you can always use the alias. It would however be a problem if one of the members was a pointer to this type.
If for example you tried to do something like this:
typedef struct {
int count;
TNODE *left, *right;
} TNODE;
This wouldn't work because the type TNODE is not yet defined at the point it is used, and you can't use the tag of the struct (i.e. the name that comes after the struct keyword) because it doesn't have one.
The difference between these two typedef declarations
typedef struct tnode TNODE;
struct tnode {
int count;
TNODE *left, *right;
};
TNODE s, *sp;
and
typedef struct {
int a;
int b;
} ab_t;
. is that in the second case you declared an unnamed structure. It means that within the structure you can not refer to itself. For example you can not write
typede struct {
int count;
TNODE *left, *right;
} TNODE;
because the name TNODE used in this member declaration
TNODE *left, *right;
is not declared yet.
But you can refer to the structure if the structure tag will have a name like
struct tnode {
int count;
struct tnode *left, *right;
};
because the name struct tnode was already declared.
Another difference is that to declare a pointer to a structure there is no need to have a complete definition of the structure. That is you may write
typedef struct tnode TNODE;
TNODE *sp;
struct tnode {
int count;
TNODE *left, *right;
};
Pay attention to that you may write a typedef declaration also the following way
struct tnode {
int count;
struct tnode *left, *right;
} typedef TNODE;
This is probably a stupid question, or it hinges on preference, or it's so obvious/often-asked that I should be ashamed. I'll accept whatever rebuke you deem appropriate, because I couldn't search up an answer.
When should I use a typedef?
One of the first questions I asked on here involved the use of a typedef on a struct where I used the _t suffix. One of the mods politely pointed out that I shouldn't be using said suffix because it is reserved for POSIX types, and (in my opinion more importantly) there is nothing wrong with just typing struct each time I reference my structure. But, of course, I'm too lazy for that, and so I've been using the preprocessor to #define a shortened struct call. Is this appropriate? Is there any advantage to using a typedef? Are there case specific instances of it being advantageous? Or would I just be risking namespace pollution? Do I risk making my code less readable by avoiding the typedef keyword?
TL;DR: Which of these would you condone/condemn? If either please elaborate.
/* Option 1 */
#define sVec struct oVec
/* Note: sVec is used later to avoid typing struct repeatedly */
struct oVec { /* I don't use sVec here for code clarity */
int x, y, z;
};
/* Option 2 */
typedef struct {
int x, y, z;
} tVec;
/* Generalized functions; @Vec could be sVec or tVec
but either should be used consistently throughout*/
@Vec * Vec_new()
{
@Vec *self = malloc(sizeof(@Vec));
self->x = 0;
self->y = 0;
self->z = 0;
return self;
}
void Vec_dst(@Vec *self)
{
free(self);
return;
}
/* Insert other useful functions like Vec_Get_x, Vec_Set_x,
and similar for y and z */EDIT: formatting, spellchecking, minor clarifications, typo fixing, style decisions...