Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > typedef vs #define

Reply
Thread Tools

typedef vs #define

 
 
riccardo
Guest
Posts: n/a
 
      10-05-2010
Hi NG,
could you please tell me which is the best option among the following two?

/* 1) */ typedef int foodata1;

/* 2) */ #define foodata2 int

Thanks in advance.
 
Reply With Quote
 
 
 
 
August Karlstrom
Guest
Posts: n/a
 
      10-05-2010
On 2010-10-05 14:20, riccardo wrote:
> Hi NG,
> could you please tell me which is the best option among the following two?
>
> /* 1) */ typedef int foodata1;
>
> /* 2) */ #define foodata2 int
>
> Thanks in advance.


http://c-faq.com/decl/typedefvsdefine.html


/August

--
The competent programmer is fully aware of the limited size of his own
skull. He therefore approaches his task with full humility, and avoids
clever tricks like the plague. --Edsger Dijkstra
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-05-2010
riccardo <(E-Mail Removed)> writes:

> could you please tell me which is the best option among the following two?
>
> /* 1) */ typedef int foodata1;
>
> /* 2) */ #define foodata2 int


Most people will go for 1. It is the proper way to write a type
definition. There will be circumstances where 2 is useful (for example
if you need to string-ify the type or you need to construct macros that
incorporate the type name) but you don't give any context and these are
unusual situations.

--
Ben.
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      10-05-2010
On Oct 5, 7:20*am, riccardo <(E-Mail Removed)> wrote:
> Hi NG,
> could you please tell me which is the best option among the following two?
>
> * */* 1) */ typedef int foodata1;
>
> * */* 2) */ *#define foodata2 int
>
> Thanks in advance.


If you need to create a synonym for a type name, use typedef (option
1); that's what it's there for. For simple types like "int" or
"double" it's hard to see a difference, but when you have something
truly ugly like

double *(*(*f)())[10];

the typedef solution is the safer and easier way to go:

typedef double *(*ptrArrPtrDbl)[10]; // ptrArrPtrDbl== double
*(*)[10]
typedef ptrArrPtrDbl (*uglyFuncPtr)(); // uglyFuncPtr == double
*(*(*)())[10]
...
uglyFuncPtr myUglyFuncPtr;
...
dblPtrArrPtr myArr = myUglyFuncPtr(); // or (*myUglyFuncPtr)();

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CRTP-problem: How can the base class typedef a derived class' typedef? oor C++ 0 05-20-2008 12:39 PM
java needs typedef Steve Green Java 11 03-25-2005 09:52 AM
Typedef of a template? Richard van Wegen C++ 3 07-15-2003 07:22 AM
template typedef as return type Robert A. T. Kaldy C++ 1 07-09-2003 06:25 PM
typedef enum qazmlp C++ 2 07-02-2003 11:55 AM



Advertisments