Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   #define vs typedef (http://www.velocityreviews.com/forums/t611614-define-vs-typedef.html)

sophia 04-27-2008 07:44 AM

#define vs typedef
 
Dear all,

the following are the differences b/w #define and typedef ,which i
have seen in Peter van der lindens book. is there any other difference
between thes two ?



The right way to think about typedef as being a complete encapsulated
type - you can't add to it after you have declared it.

ex:-

#define peach int
unsigned peach i; /*works fine*/

typedef int banana ;
unsigned banana i; /*illegal*/

a typedef'd name provides the type for every declarator in a
declaration

Ex:-

#define int_ptr int*
int_ptr chalk, cheese;

after macro expansion, the second line effectively becomes
int* chalk,cheese;

In contrast a typedef like this:

typedef char* char_ptr;
char_ptr bentley,rolls_royce;

declares both bentley and rolls_royce to be the same . the name on the
front is different, but they are both a pointer to a char.



All times are GMT. The time now is 12:31 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.