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;
Discussion:
10 comments Page 1 of 1.
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;
{
int data;
NODEPTR link; //NODEPTR is invalid
}*NODEPTR;
we should use:
typedef struct node
{
int data;
struct node *link;
}NODEPTR;
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.
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.
Manisha said:
9 years ago
Can anyone explain clearly?
Sneha said:
1 decade ago
Need correct explanation.
Inuom said:
1 decade ago
How can we use typedef struct in the declaration itself? Can anyone explain clearly?
Umesh said:
1 decade ago
typedef can not be used until it is defined.
Karthik said:
1 decade ago
It is defined only when a name is given or it is said to be anonymous.
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.
So, it is used earlier.
Sai said:
1 decade ago
Typedef is used in the place of a declaration or type specifiers such as int, float.
Apurva Nigam said:
1 decade ago
Can any one explain??
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers