typedef and #define

C

Chris Dollin

vivek said:
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?
 
J

John Bode

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.
 
M

Morris Dovey

John said:
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?
 
K

Kaz Kylheku

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.

:)
 
J

John Bode

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.
 
B

Bartc

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))

?
 
J

John Bode

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.
 
M

Morris Dovey

Micah said:
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. :)
 
M

Micah Cowan

Morris Dovey said:
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.
 
K

Kenneth Brody

vivek said:
What will happen if i replace a typedef with a #define?

Your program will fail to compile, your girlfriend will leave you,
your hair will fall out, and your father will smell of elderberry
wine.

Or simply consider what happens if you replace:

typedef char *CHAR_PTR;
with
#define CHAR_PTR char *

and your program includes:

CHAR_PTR pt1, pt2;

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
R

Richard Bos

Morris Dovey said:
Heh - might as well invoke all the demons...

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

I don't know, but have you SEEN the CHAIR recently?

Richard
 
A

ais523

Bartc said:
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))

?
It's (int *(*(*)[20])(char)) because you can create a cast from a
declaration by removing the identifiers in it that are referring to
variables and parameters (unless someone has decided to do #define c
volatile, or something like that, in which case the c would be
required because it's qualifying the char that each of the functions
takes as an argument, but generally speaking it can be assumed that
the preprocessor isn't involved in this sort of question). (I'm not
sure if (without the #define) it works as char c as well as with char,
though.) If I ever had to write that, I'd explain what it did in a
comment, though, I suspect (unless it was for the IOCCC). The only
reason I can think of for using that cast right now would be
dereferencing a pointer that was actually pointing to that bizarre
collection of function pointers but was stored as a void* for some
reason, but in such a case there's something to be said for assigning
it to a variable of the right type and dereferencing that so as to
make use of automatic void* conversion, and to not get confused. Oh,
also casting the return value of malloc, but that's a bad idea anyway
unless for some reason you want your C code to also be valid C++ (and
normally you don't).
 
M

Morris Dovey

Richard said:
I don't know, but have you SEEN the CHAIR recently?

I'm obviously missing something (either I've been away too long
or didn't get enough sleep last night) - what have I missed?
(e-mail is ok if you prefer).
 
M

Morris Dovey

Richard said:
I don't know, but have you SEEN the CHAIR recently?

I'm obviously missing something (either I've been away too long
or didn't get enough sleep last night) - what have I missed?
(e-mail is ok if you prefer).
 
R

Richard Bos

Morris Dovey said:
I'm obviously missing something (either I've been away too long
or didn't get enough sleep last night) - what have I missed?
(e-mail is ok if you prefer).

Xney Z., our erstwhile local KOOK-IN-ITSELF.

Richard
 
R

Richard Heathfield

Morris Dovey said:
I'm obviously missing something (either I've been away too long
or didn't get enough sleep last night) - what have I missed?

Don't you remember Karl Malbrain? You replied to him at least once...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

#define IDLE_WORK() { ; } 2
Please explain: #define MOVE 0x05 1
typedef, 2D arrays, design: question 11
const vs typedef 8
Ways to define C constants 46
Help with #define 0
typedef struct 39
Typedef and const 4

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top