More on structures and unions

typedef does not define a new data type
There are three main reasons for using typedefs:
* It makes the writing of complicated declarations a lot easier. This helps in eliminating a lot of clutter in the code.
* It helps in achieving portability in programs. That is, if we use typedefs for data types that are machine dependent, only the typedefs need to change when the program is ported to a new platform.
* It helps in providing better documentation for a program. For example, a node of a doubly linked list is better understood as ptrToList than just a pointer to a complicated structure.

More on typedef

1. typedef struct date{
int day;
int month;
};

For such type of declaration, during instantiation we should use “struct date Birthday”
2. typedef struct{
int day;
int month;
}date;

And in this case, instantiation can be done using “date Birthday” no struct key word is required
The above two are in different namespaces;

typedef struct date{int day,int month,date next}date;
This declaration is prefectly valid because, all the three are in different namespaces. The three usages of name date can be distinguishable by the compiler at any instance, so valid;
The first date can only be used if its declaration is preceeded by struct keyword, e.g. struct date Bday
The second date (next), can be used only after . (dot) or -> (arrow) operator preceded by the variable
The third one, can only be used to define variables without using the preceding struct keyword e.g

Leave a Reply

Your email address will not be published. Required fields are marked *