Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   typedef Vs #define (http://www.go4expert.com/forums/typedef-vs-define-t13405/)

kanaks_go4ex 29Aug2008 23:52

typedef Vs #define
 
Hello guys,

What is the difference between typedef and #define?

thanks
-kanaks

oogabooga 30Aug2008 20:47

Re: typedef Vs #define
 
There are two differences between define and typedef.

Firstly, typedef obeys scoping rules just like variables, whereas define
stays valid until the end of the file (or until a matching undef).

Secondly, some things can be done with typedef that cannot be done with define.
Examples:
Code:

typedef int* int_p1;
int_p1 a, b, c;  // a, b, and c are all int pointers.

#define int_p2 int*
int_p2 a, b, c;  // only the first is a pointer!

Code:

typedef int a10[10];
a10 a, b, c; // create three 10-int arrays

Code:

typedef int (*func_p) (int);
func_p fp // func_p is a pointer to a function that
          // takes an int and returns an int


kanaks_go4ex 2Sep2008 00:06

Re: typedef Vs #define
 
cool..!
thanks.


All times are GMT +5.5. The time now is 16:46.