Thursday, December 29, 2011

typedef operator in C

Discuss the significance of typedef operator in C

typedef is like #define,except that since it is interpreted by the compiler,it can cope with textual substituitions that are beyond the capabilities of the preprocessor.
typedef int (*PFI) (char *,char*);
creates the type PFI for pointers to functon(of two char * arguments) returning int which can be used in contexts like PFI strcmp, numcmp;


Main Advantages of typedef:
1.This is used to parametrize a program against portability problems.If typedefs are used for data types that may be machine dependent only the typedefs need change when the program is moved.One common situation is to use typedef names for various integer quantities then make an appropriate set of choices of short,int and long for each host ,machine.Types like size_t & ptrdiff_t from standard library are examples
2.It provides better documentation for a program-a type called Treeptr may be easier to understand than one declared only as a pointer to a complicated structure.

Useful Links:
http://www.oualline.com/style/c06.html
http://itee.uq.edu.au/~comp2303/Leslie_C_ref/C/SYNTAX/typedef.html
http://msdn.microsoft.com/en-us/library/4x7sfztk.aspx
http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc03sc03.htm

1 comment:

  1. http://publications.gbdirect.co.uk/c_book/chapter8/typedef.html

    ReplyDelete