#define vs typedef

S

sophia

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top