Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

typedef and #define

 
 
vivek
Guest
Posts: n/a
 
      03-04-2008
What will happen if i replace a typedef with a #define?
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      03-04-2008
vivek wrote:

> What will happen if i replace a typedef with a #define?


See:

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

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      03-04-2008
vivek wrote:

> What will happen if i replace a typedef with a #define?


Your code will break [1].

[1] Probably. Why court disaster? Why replace the right construct with
the wrong one?

--
"Ashes are burning the way." - Renaissance, /Ashes Are Burning/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      03-04-2008
On Mar 4, 4:56 am, vivek <(E-Mail Removed)> wrote:
> What will happen if i replace a typedef with a #define?


It depends. Let's suppose you've created a typedef for a pointer to
int:

typedef int *iptr;
iptr px, py;

Both px and py will be typed int*.

Now we replace it with a macro:
#define IPTR int *
IPTR px, py;

After preprocessing, this will expand to
int * px, py;

In this case, only px will be typed int*; py will be a plain int.

So, for this particular case, you get different results.

Some typedefs would be difficult to replace with a macro. For
example, take

typedef int *(*(*foo)[20])(char c);

foo is a synonym for "pointer to 20-element array of pointers to
functions taking a char parameter and returning a pointer to int." A
simple macro to replace this typedef would be, well, not so simple.
 
Reply With Quote
 
Morris Dovey
Guest
Posts: n/a
 
      03-04-2008
John Bode wrote:
>
> On Mar 4, 11:44 am, Kaz Kylheku <(E-Mail Removed)> wrote:
> > On Mar 4, 2:56 am, vivek <(E-Mail Removed)> wrote:
> >
> > > What will happen if i replace a typedef with a #define?

> >
> > Nothing of consequence.
> >
> > Eminent computer scientist S**** N**** demonstrated, years ago, that
> > they are the same thing.
> >
> >

>
> DO NOT SPEAK THAT NAME, lest he notice and come back to pester us
> again, with the Three Stooges (Twink, McCormack, and Just Richard)
> egging him on.


Heh - might as well invoke all the demons...

What ever happened to that lad who preached that hexadecimal was
the one true number system?

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      03-04-2008
On Mar 4, 2:56*am, vivek <(E-Mail Removed)> wrote:
> What will happen if i replace a typedef with a #define?


Nothing of consequence.

Eminent computer scientist Scott Nudds demonstrated, years ago, that
they are the same thing.


 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      03-04-2008
On Mar 4, 11:44 am, Kaz Kylheku <(E-Mail Removed)> wrote:
> On Mar 4, 2:56 am, vivek <(E-Mail Removed)> wrote:
>
> > What will happen if i replace a typedef with a #define?

>
> Nothing of consequence.
>
> Eminent computer scientist S**** N**** demonstrated, years ago, that
> they are the same thing.
>
>


DO NOT SPEAK THAT NAME, lest he notice and come back to pester us
again, with the Three Stooges (Twink, McCormack, and Just Richard)
egging him on.
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      03-04-2008

"John Bode" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Mar 4, 4:56 am, vivek <(E-Mail Removed)> wrote:
>> What will happen if i replace a typedef with a #define?


> Some typedefs would be difficult to replace with a macro. For
> example, take
>
> typedef int *(*(*foo)[20])(char c);


>foo is a synonym for "pointer to 20-element array of pointers to
>functions taking a char parameter and returning a pointer to int."


Are you saying that:

typedef int *(*(*foo)[20])(char c);

Creates a complex type with an alias of foo; while:

int *(*(*bar)[20])(char c);

Creates a variable bar with that same type? (Not sure what the 'c' is doing
in there).

How would I specify that same complex type without either the foo or bar
tag, as in a cast for example? Is it just:

(int *(*(*)[20])(char c))

?

--
Bart




 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      03-04-2008
On Mar 4, 12:23 pm, "Bartc" <(E-Mail Removed)> wrote:
> "John Bode" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > On Mar 4, 4:56 am, vivek <(E-Mail Removed)> wrote:
> >> What will happen if i replace a typedef with a #define?

> > Some typedefs would be difficult to replace with a macro. For
> > example, take

>
> > typedef int *(*(*foo)[20])(char c);
> >foo is a synonym for "pointer to 20-element array of pointers to
> >functions taking a char parameter and returning a pointer to int."

>
> Are you saying that:
>
> typedef int *(*(*foo)[20])(char c);
>
> Creates a complex type with an alias of foo; while:
>
> int *(*(*bar)[20])(char c);
>
> Creates a variable bar with that same type?


Yes.

> (Not sure what the 'c' is doing in there).


Documentation.

>
> How would I specify that same complex type without either the foo or bar
> tag, as in a cast for example? Is it just:
>
> (int *(*(*)[20])(char c))
>


I *think* that's right, but I won't swear to it. Hopefully you should
never need such a beast; if you do, that's where the typedef would
save your bacon. Bear in mind, that's a deliberately pathological,
made-up example to show where using a #define instead of a typedef
would fail.

> ?
>
> --
> Bart


 
Reply With Quote
 
Morris Dovey
Guest
Posts: n/a
 
      03-04-2008
Micah Cowan wrote:
>
> Morris Dovey <(E-Mail Removed)> writes:


> > Heh - might as well invoke all the demons...

>
> Ah, so _those_ are what flew out my nose when I forgot to ensure
> isalpha()'s argument was representable as an unsigned char.


Been wondering what you've been doing <g>

I thought it might've been all my fault - I've been developing a
DS.009 whose stdin, stdout, stderr, root partition, <everything>
is hot-swappable (see web page at sig) so that they need to be
continually "rediscovered" - and I've been suspecting that our
favorite redhead might have expanded red/black to
red/orange/yellow/green/blue/black and promulgated severe
dimensional instabilities leading to the strange conjunction of
all moons in the solar system waxing to fullness at the same
instant.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/Projects/Monitor/
 
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
function template overloading and typedef in GCC Arkadiy Vertleyb C++ 5 09-27-2003 12:28 PM
A question about typedef and classes and templates.. JustSomeGuy C++ 1 09-20-2003 04:33 AM
typename, typedef, and resolution Gina Yarmel C++ 4 08-13-2003 04:06 PM
typedef and #ifdef F. Edward Boas C++ 1 08-11-2003 05:52 PM



Advertisments