By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,570 Members | 1,310 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,570 IT Pros & Developers. It's quick & easy.

#define vs typedef

P: n/a
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.

Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Sun, 27 Apr 2008 00:44:41 -0700 (PDT), sophia
<so**********@gmail.comwrote in comp.lang.c:
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.
cv qualifiers (and possibly the restrict qualifier, haven't thought
about it) can't "penetrate" a typedef, they can only apply to the
object defined.

Example:

typedef char *cptr;
#define CPTR char *

int main(void)
{
char junk [] = "abcdefghij";
const cptr cp1 = junk;
cptr const cp2 = junk;
const CPTR cp3 = junk;
CPTR const cp4 = junk;
*cp1 = '1'; /* line 11, cp1 points to non-const char */
*cp2 = '2'; /* line 12, cp2 points to non-const char */
*cp3 = '3'; /* line 13, cp3 points to const char */
*cp4 = '4'; /* line 14, cp4 points to non-const char */
++cp1; /* line 15, cp1 pointer is const */
++cp2; /* line 16, cp2 pointer is const */
++cp3; /* line 17, cp3 pointer is non-const */
++cp4; /* line 18, cp4 pointer is const */
}

Here's the output from Visual C++ 6:

junk.c(13) : error C2166: l-value specifies const object
junk.c(15) : error C2166: l-value specifies const object
junk.c(16) : error C2166: l-value specifies const object
junk.c(18) : error C2166: l-value specifies const object

So putting the const keyword either before or after a typedef'ed
pointer type does not make it a pointer to const" as one might think.
Instead the pointer being defined, is a constant pointer to
non-constant.

On the other hand, adding the const keyword to the CPTR macro makes is
a non-const pointer to const car, if used before the macro, or a const
pointer to non-const char, if used after.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.