Why typedef is handy ?
Consider this....
If you want two pointer variables ( lets say of type int * ), how will you declare it?
--> int* ptr1,ptr2; #WRONG
If we follow the above syntax, the compiler will allocate 2 space
( i ) a pointer variable ptr1 which will store an address of INTEGER VARIABLE ADDRESS.
( ii ) a normal variable ptr2 which will store an INTEGER VALUE.
To avoid this ,
METHOD 1: int * ptr1, *ptr2;
or ,
Use Typedef.
: It is used to give alternate name to your data types.
Syntax: typedef data_type alternate_name_for_that_datatype;
eg,
typedef int* intPointer;
this way, you can declare
intPointer ptr1,ptr2;
Now, consider this,
Suppose you are working with Linked Lists or any other ADTs, or structures,
You must have noticed, those data types name can become lengthy
ie, struct node, struct myPersonalInformation, etc.
now, consider reserving a space for those datatypes.
ie, struct myPersonalInformation name1;
What if u could avoid all that??...
typedef struct myPersonalInformation myself;
now, you have an alternate name for your data type.
NOTE:
Even if you declare an alternate name for your data type, you can still use the original name.
good content fill us with more knowledge like this
ReplyDelete