Reputation: 45
I am getting confused with typedef can anyone transcribe this to a normal composition? of structures? I really don't want to handle typedef since it's gets me confuse
struct stackNode
{
int data;
struct stackNode *nxtptr;
};
typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;
is
typedef struct stackNode StackNode;
is the same as struct stackNode StackNode
and typedef StackNode *StackNodePtr;
is the same as struck stackNode *StackNodePtr
??
Upvotes: 3
Views: 22755
Reputation: 923
typedef struct stackNode { int data; struct stackNode *nxtptr; } StackNode_t;
If you do not want to have "struct stackNode" inside the struct, I found that I am able to do this:
typedef struct STACKNODE STACKNODE;
typedef struct STACKNODE
{
int data;
STACKNODE *nxtptr;
};
That you could code such a thing, AND it compiles just fine, AND it runs just fine may seem counter intuitive. It can be counter intuitive because I'm using the same name for struct STACKNODE and the typedef STACKNODE. And further, I am typedef-ing the struct before it exists as a definition. Nevertheless, I have found that I can do this with Microsoft's C through many versions to today and Borland C (way back then).
I like it because if I am already going to typedef the struct, I don't like resorting to "struct STACKNODE *nxtptr" (i.e. using the word "struct") inside the struct definition.
Upvotes: 2
Reputation: 215115
A more common way to write the very same would be:
typedef struct stackNode
{
int data;
struct stackNode *nxtptr;
} StackNode_t;
where stackNode
is the so called "struct tag" and StackNode_t
is the actual name of the type. If you declare structs like this, the rest of the program won't need to concern itself with the struct tag, and you can use nxtptr as if it was of StackNode_t.
Upvotes: 7
Reputation: 4441
see if typedef
helps. More like a 101. The section labelled "Usage concerns" may give some insights into your, er, concerns.
Upvotes: 0
Reputation: 2060
Well, to explain it easily: Typedef does basically nothing else then to tell the compiler that you create a new type of variables (such as int, char etc..)
The main reasons why to use typedef are
Upvotes: 1
Reputation: 26329
With typedef, you define StackNode to be struct stackNode, and the Pointer StackNodePtr to be StackNode.
So what is not clear?
Upvotes: 1
Reputation: 181460
If you don't want to use typedef
you can always use full type name:
Instead of:
StackNode sn;
You would use:
struct stackNode sn;
Instead of:
StackNodePtr snp;
You would use:
struct stackNode *snp;
The declarations are exactly the same.
Upvotes: 7