C Programming - Typedef - Discussion
Discussion Forum : Typedef - Point Out Errors (Q.No. 1)
1.
In the following code snippet can we declare a new typedef named ptr even though struct employee has not been completely declared while using typedef?
typedef struct employee *ptr;
struct employee
{
char name[20];
int age;
ptr next;
}
Discussion:
10 comments Page 1 of 1.
SHAHIDNX said:
1 decade ago
Since we are performing typedef for pointer variable so it is valid.
Abhayraj said:
1 decade ago
What are the uses /applications of typedef? Why it is used with structure many times? please, explain.
Gaurav Garg said:
1 decade ago
@Abhayraj.
Uses and application of typedef is only for making better understanding of code and instead of using many word we can use only single word. That it only.
e.g.
typedef const unsigned char U_CHAR
Here we can use U_CHAR instead of three word name " const unsigned char ".
Uses and application of typedef is only for making better understanding of code and instead of using many word we can use only single word. That it only.
e.g.
typedef const unsigned char U_CHAR
Here we can use U_CHAR instead of three word name " const unsigned char ".
Suman Saurabh said:
1 decade ago
typedef struct employee *ptr;
This statement is written before employee structure is defined so why it will not give an error ? Please Explain.
This statement is written before employee structure is defined so why it will not give an error ? Please Explain.
Yashwanth said:
10 years ago
The Typedef is used for giving an alternative names for the existing types.
Ex:
main()
{
typedef int INT;
//Here instead of int i can use INT,
INT a=2,b=3,c;
c=a+b;
printf("%d",c);
}
Ex:
main()
{
typedef int INT;
//Here instead of int i can use INT,
INT a=2,b=3,c;
c=a+b;
printf("%d",c);
}
(1)
Sweety said:
9 years ago
Please, anyone explain clearly.
V r prathap said:
9 years ago
Yes, Right @Suman Saurabh.
Why it will not give an error?
Why it will not give an error?
Shivani Gupta said:
8 years ago
Yes, please explain it for me.
Chinmay said:
7 years ago
"typedef struct employee *ptr; ".
It's self-referential structure concept. Compiler read this line and assigned 4bytes for it, because of its pointer. At that time compiler does not require any data type. If there is no pointer only (variable_name) is present then it will throw an error.
It's self-referential structure concept. Compiler read this line and assigned 4bytes for it, because of its pointer. At that time compiler does not require any data type. If there is no pointer only (variable_name) is present then it will throw an error.
Mahalakshmi said:
4 years ago
Declaring a self-referential structure, before the structure is completely declared is accepted in C.
Whereas declaring a normal variable using a typedef name before the complete declaration of structure throws an error.
typedef struct stu
{
int x;
char c;
ST record; //error typedef name used before the complete declaration of structure.
}ST;
typedef struct stu
{
int x;
char c;
ST* record;//valid since it's a self-referential pointer.
}ST;
Whereas declaring a normal variable using a typedef name before the complete declaration of structure throws an error.
typedef struct stu
{
int x;
char c;
ST record; //error typedef name used before the complete declaration of structure.
}ST;
typedef struct stu
{
int x;
char c;
ST* record;//valid since it's a self-referential pointer.
}ST;
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers