Using Typedef With Structures

Introduction :
  1. It allows us to introduce synonyms for data types which could have been declared some other way.
  2. It is used to give new name for the structure.
  3. New name is used for Creating instances,passing values to function,declaration etc...
typedef int XYZ;  
XYZ age;
XYZ roll;
Explain ?
  1. New name given for integer is XYZ.
  2. 'XYZ' and 'int' are now synonyms.
  3. 'age' is of type XYZ i.e of integer type.

Example 1 :
typedef struct
{
char ename[30];
int ssn;
int deptno;
}employee;
Example 2 :
typedef struct Rec
{
char ename[30];
int ssn;
int deptno;
}employee;
What we have done ?
  1. Consider Example 2, Rec is tag-name.
  2. 'employee' is nothing but new data type.
  3. We can now create the variables of type 'employee'
  4. Tag name is optional .

Live Example :
typedef struct b1
{
char bname[30];
int ssn;
int pages;
}book;
book b1,b2,b3; 


Bookmark & Share


Tags / Keywords : | ,

0 comments

Post a Comment

Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email