Typedef for basic type :-
- Typedefs can make your code more
One purpose of having types is to make sure that variables are always used in the way that they were intended to be used. C and C++ provide several built-in types that are based on the underlying representation of the data. Sometimes this is good enough; but when you're working to save memory or perform numerical computations that require the right level of precision, you want to have tight control over these matters. This constraint is to actually create "wrapper" classes for the different types. This allows your compiler to spot incorrect assignments and prevent you from making them.
error_t get_data( char *data );
Where error_t is typed as:
unsigned int error_t;
- Typedefs can make your code easier to modify
Typedefs provide a level of abstraction away from the actual types being used, allowing you, the programmer, to focus more on the concept of just what a variable should mean. This makes it easier to write clean code, but it also makes it far easier to modify your code.
Example: If you decided you really needed to support sizes that were too big to store in an unsigned int, you could make a change in one place in your code--the typedef itself--to make size_t equivalent to, for instance, an unsigned long. Almost none of your code would need to change!
Some people are opposed to the extensive use of typedefs. Most arguments center on the idea that typedefs simply hide the actual data type of a variable. For example, Greg Kroah-Hartman, a Linux kernel hacker and documenter, discourages their use for anything except function prototype declarations. He argues that this practice not only unnecessarily obfuscates code, it can also cause programmers to accidentally misuse large structures thinking them to be simple types.
Others argue that the use of typedefs can make code easier to maintain. K&R states that there are two reasons for using a typedef. First, it provides a means to make a program more portable. Instead of having to change a type everywhere it appears throughout the program's source files, only a single typedef statement needs to be changed. Second, a typedef can make a complex declaration like pointers, structures, function pointer etc easier to understand.
Hope this will help you.