Reputation: 123
I don't quite understand why this structure is defined this way.
here is the block of code in question...
typedef struct Except_Frame Except_Frame;
struct Except_Frame {
Except_Frame *prev;
jmp_buf env;
const char *file;
int line;
const T *exception;
};
Why is this struct defined this way, as opposed to just ...
typedef struct {
Except_Frame *prev;
jmp_buf env;
const char *file;
int line;
const T *exception;
} Except_Frame;
and what are the advantages?
Upvotes: 2
Views: 221
Reputation: 4880
First we need to understand the use of typedef; typedef can be used to indicate how a variable represents something; Example
typedef int km_per_hour ;
typedef int points ;
Now, coming to your case, you are defining the struct and you want it to typedef to call by something; We need to PREDEFINE this before using it thus we are declaring before defining the struct
1 typedef struct Except_Frame t_Except_Frame;
2 struct Except_Frame {
3 t_Except_Frame *prev;
4 ...
5 }
line 1) Now the compiler understand that there will be struct of name "struct Except_Frame" and we need to typedef to "t_Except_Frame"; Did you note that I added t_ for typedef; it is good practice to follow this so that it would be easy for programmer to understand that this value is typedef;
line 3) The system understand it is typedef variable of struct Except_Frame and compile the program accordingly.
Upvotes: 0
Reputation: 320777
If you need that typedef name, then the two popular stylistic variants available to you actually look as follows
typedef struct Except_Frame Except_Frame;
struct Except_Frame {
Except_Frame *prev;
...
};
or
typedef struct Except_Frame {
struct Except_Frame *prev;
...
} Except_Frame;
Note the difference from your second variant (and your original second variant will not even compile).
Now, which one you want to use is largely a matter of your personal preference. The first variant makes the "short" version of the type name (just Except_Frame
) available earlier than the second variant.
Upvotes: 0
Reputation: 447
By using
typedef struct Except_Frame Except_Frame;
You are renaming the struct "struct Except_Frame" into "Except_Frame".
First, it's more convenient to type Except_Frame rather than struct Except_Frame. Second, in this case, the field "Except_Frame *prev" of the struct will fail in compilation as the compiler is not familiar with a struct called "Except_Frame" (It is familiar with a struct called struct Except_Frame)
Cheers, N
Upvotes: 1
Reputation: 206747
If you don't use:
typedef struct Except_Frame Except_Frame;
then, the struct
will need to be defined using:
struct Except_Frame {
// The keyword struct is necessary without the typedef
struct Except_Frame *prev;
jmp_buf env;
const char *file;
int line;
const T *exception;
};
If you want to define the struct
and the typedef
in one statement, it will be:
typedef struct Except_Frame {
// The keyword struct is necessary without the typedef
// being defined ahead of the definition of the struct.
struct Except_Frame *prev;
jmp_buf env;
const char *file;
int line;
const T *exception;
} Except_Frame;
Upvotes: 4