C Programming - Typedef - Discussion

Discussion Forum : Typedef - Point Out Errors (Q.No. 2)
2.
Point out the error in the following code?
typedef struct
{
    int data;
    NODEPTR link;
}*NODEPTR;
Error: in *NODEPTR
Error: typedef cannot be used until it is defined
No error
None of above
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
10 comments Page 1 of 1.

Apurva Nigam said:   1 decade ago
Can any one explain??
(1)

Sai said:   1 decade ago
Typedef is used in the place of a declaration or type specifiers such as int, float.

Nayan rath said:   1 decade ago
Actully NODEPTR is the name to which typedef indicates. So after declaration only we can use in the function.

So, it is used earlier.

Karthik said:   1 decade ago
It is defined only when a name is given or it is said to be anonymous.

Umesh said:   1 decade ago
typedef can not be used until it is defined.

Inuom said:   1 decade ago
How can we use typedef struct in the declaration itself? Can anyone explain clearly?

Sneha said:   1 decade ago
Need correct explanation.

Manisha said:   9 years ago
Can anyone explain clearly?

Mohit Rajput said:   9 years ago
You all wondering about correct explanation. Let us start.

1. We can make a structure like this but point to be noted is typedef struct

{
int data;
NODEPTR link; // ----point to be noted----
}*NODEPTR;

In C language, we can not make a structure variable inside the structure of the same type.

(NODEPTER link) NODEPTER is a name of structure and link is the name of the variable of structure and we can't make a variable of structure inside a structure . if we do this thing the compiler is getting confused about the size of the structure.

2. But we can make a pointer variable of struct type example.
typedef struct
{
int data;
NODEPTR *link; // this is correct this type of structure we called self referential strucutre.
} NODEPTR; // this is the name of structure.

This type of structure is called self-referential structure.

Zdd said:   7 years ago
typedef struct
{
int data;
NODEPTR link; //NODEPTR is invalid
}*NODEPTR;


we should use:
typedef struct node
{
int data;
struct node *link;
}NODEPTR;

Post your comments here:

Your comments will be displayed after verification.