Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   typedef and #define (http://www.velocityreviews.com/forums/t595935-typedef-and-define.html)

vivek 03-04-2008 10:56 AM

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

santosh 03-04-2008 10:59 AM

Re: typedef and #define
 
vivek wrote:

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


See:

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


Chris Dollin 03-04-2008 12:25 PM

Re: typedef and #define
 
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


John Bode 03-04-2008 05:26 PM

Re: typedef and #define
 
On Mar 4, 4:56 am, vivek <gvivek2...@gmail.com> 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.

Morris Dovey 03-04-2008 05:31 PM

Re: typedef and #define
 
John Bode wrote:
>
> On Mar 4, 11:44 am, Kaz Kylheku <kkylh...@gmail.com> wrote:
> > On Mar 4, 2:56 am, vivek <gvivek2...@gmail.com> 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

Kaz Kylheku 03-04-2008 05:44 PM

Re: typedef and #define
 
On Mar 4, 2:56*am, vivek <gvivek2...@gmail.com> 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.

:)

John Bode 03-04-2008 06:15 PM

Re: typedef and #define
 
On Mar 4, 11:44 am, Kaz Kylheku <kkylh...@gmail.com> wrote:
> On Mar 4, 2:56 am, vivek <gvivek2...@gmail.com> 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.

Bartc 03-04-2008 06:23 PM

Re: typedef and #define
 

"John Bode" <john_bode@my-deja.com> wrote in message
news:81264305-222f-4c7f-9551-e6a84f017ea5@v3g2000hsc.googlegroups.com...
> On Mar 4, 4:56 am, vivek <gvivek2...@gmail.com> 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





John Bode 03-04-2008 06:34 PM

Re: typedef and #define
 
On Mar 4, 12:23 pm, "Bartc" <b...@freeuk.com> wrote:
> "John Bode" <john_b...@my-deja.com> wrote in message
>
> news:81264305-222f-4c7f-9551-e6a84f017ea5@v3g2000hsc.googlegroups.com...
>
> > On Mar 4, 4:56 am, vivek <gvivek2...@gmail.com> 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



Morris Dovey 03-04-2008 08:20 PM

Re: typedef and #define
 
Micah Cowan wrote:
>
> Morris Dovey <mrdovey@iedu.com> 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/


All times are GMT. The time now is 07:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.