473,888 Members | 2,199 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

#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_r oyce;

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
1 5228
On Sun, 27 Apr 2008 00:44:41 -0700 (PDT), sophia
<so**********@g mail.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_r oyce;

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.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
19264
by: lallous | last post by:
Hello I noticed that when I use #define inside a namespace then this #define only works when prefixed with the namespace name. Is this behaviour standard or just compiler specific? I use VC7. namespace test
42
5639
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
10
7520
by: O Plameras | last post by:
Are there differences in terms of functionality of, #define and typedef ? By the above I mean any instance when the outcome obtained by running two versions (1) and (2) below of C codes are different ?
3
9811
by: robin liu | last post by:
What's the difference between these two declarations ? 1) typedef void (*pf)(void); 2) typedef void f(void); the first declaration is define a function pointer, what is the second ? define a function model? And can use the second declaration to define a function pointer as follow:
10
3928
by: Christian Christmann | last post by:
Hi, what is the difference between a "#define" and typedef? A "#define" is handled by the preprocessor and serves as a name substitude whereas "typedef" is a declaration of a new data type based on an exisiting one and handled by a compiler. So, actually both things are equivalent? Or are there any special cases where it makes a difference which specifier is used?
71
33274
by: David T. Ashley | last post by:
Where is the best place to define TRUE and FALSE? Are they in any of the standard include files, ever? Do any standards apply? What I've traditionally done is something like: #ifndef (TRUE) #define TRUE (1)
23
3934
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
6
2167
by: wongjoekmeu | last post by:
I need to rewrite some typedef to #define. For instance I rewrote typedef void* handle to #define handle void* But I saw for instance another typedef in my code which I don't understand, which is
2
1440
by: ds | last post by:
Hi all, what I try to do is the following: template<class Tpclass themap { public: typedef int Tp::*ptr; static std::map<std::string, Tp::ptrsmap;
0
9957
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9799
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10772
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10434
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7988
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7143
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4635
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.