define versus typedef

V

Vladimir Oka

Christian said:
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?

Well, you answered this yourself above. Obviously different beasts
altogether.
Or are there any special cases where it makes a difference
which specifier is used?

What if someone decides to use `#undef` on your neatly `#define`d new
type. And then `#define`s it as something altogether different. In an
`#include`d file just below yours. Without telling you.

It may be cumbersome to `#define` multiline types (think `struct` and
`union`).

I'm sure someone will come along and point other differences, and
pitfalls of using `#define` for defining new types.
 
M

Mark Odell

Christian said:
Hi,

what is the difference between a "#define" and typedef?

Typedef's have a type thus the compiler can enforce typechecking.
Typedefs can be scoped within blocks or files. Once a #define is
defined it must be undef'd to "get rid of it", that is, it doesn't
automatically undef when you leave a block scope. #defines can be many
types like float, double, long, char, a string, code blocks, etc. I'm
not sure that a typedef can be a string and it certainly can't be a
macro (code). #defines can be checked for definedness, typedefs cannot.
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.
Yup.

So, actually both things are equivalent? Or are there any special
cases where it makes a difference which specifier is used?

Specifier? Yes it makes a difference and it depends on what you want to
do.
 
C

Chris Dollin

Christian said:
what is the difference between a "#define" and typedef?

One is a homework question, and the other is a question for homework.
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.

Questions? Answers! Answers? Questions!
So, actually both things are equivalent?

Not remotely. One says "declare that this identifier means this type",
and requires that the type is well-formed and introduces a new name
into the current scope, with all that that implies plus that name
can be used as a type. The other says "where I write this name, pretend
I wrote these tokens instead".
Or are there any special cases where it makes a difference which
specifier is used?

[Only one of them is a specifier.]

There are lots of /general/ cases where it makes a difference.
 
N

Neroku

Christian Christmann ha escrito:
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?

Regards,
Chris

Consider the following example:

#define INT_P int *
INT_P a,b;
"a" is a pointer to integer whereas "b" is an integer. That is, the
code above expands to:
int *a,b;

now consider this another example:

typedef INT_P int *
INT_P a,b;
"a" and "b" are now pointers to integer. This code is equivalent to
write:
int *a,*b;

#define foo bar: the preprocessor changes in the code each foo to bar.
typedef foo bar: each variable and so on, which is declared with foo
type, is internally of bar type, done by the compiler.
 
K

Kenneth Brody

Christian said:
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?

Consider:

#define char_pt_t char *
versus
typedef char *char_pt_t;

and then the following declaration:

char_pt_t Pointer1, Pointer2;

--
+-------------------------+--------------------+-----------------------+
| 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]>
 
E

Eric Sosman

Kenneth Brody wrote On 06/08/06 10:06,:
Consider:

#define char_pt_t char *
versus
typedef char *char_pt_t;

and then the following declaration:

char_pt_t Pointer1, Pointer2;

Equally, consider

typedef int RGBcolor[3];
RGBcolor black = { 0, 0, 0 };

Your mission, should you choose to accept it, is to replace
the first line with a #define while leaving the meaning of
the second line unchanged.

For extra credit, do the same with

typedef int (*Comparator)(const void*, const void*);
struct node *sort(struct node *list, Comparator func);
 
K

Kenneth Brody

Eric Sosman wrote:
[...]
Equally, consider

typedef int RGBcolor[3];
RGBcolor black = { 0, 0, 0 };

Your mission, should you choose to accept it, is to replace
the first line with a #define while leaving the meaning of
the second line unchanged.

For extra credit, do the same with

typedef int (*Comparator)(const void*, const void*);
struct node *sort(struct node *list, Comparator func);

I've also used typedefs simply for sanity's sake.

For example, an array of pointers to functions which take a function
pointer as a parameter.

I'd much rather write something like (untested):

typedef int (*IntFunc)();
typedef int (*Whatever)(IntFunc);
Whatever MyWhateverArray[] = { foo1, foo2 };

as compared to having something like (untested):

int (*(MyWhateverArray[]))(int (*)()) = { foo1, foo2 };

I'm sure someone will be happy to correct any typos above. :)

--
+-------------------------+--------------------+-----------------------+
| 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]>
 
K

Krishanu Debnath

Christian said:
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?

Regards,
Chris

Others have already provided you the right answer. You may also find the
following link(from FAQ) useful.

http://c-faq.com/ansi/typedefconst2.html

Krishanu
 
K

Keith Thompson

Kenneth Brody said:
Christian said:
what is the difference between a "#define" and typedef?
[snip]

Consider:

#define char_pt_t char *
versus
typedef char *char_pt_t;

and then the following declaration:

char_pt_t Pointer1, Pointer2;

That's an excellent example to illustrate the point. To digress,
another, mostly unrelated, point is that creating a typedef for a
pointer type is rarely a good idea. If the code using the type needs
to know it's a pointer, don't hide the type's pointerness behind an
alias. (If the type is intended to be abstract, on the other hand, so
the code using it *shouldn't* care that it's a pointer. a typedef is a
good approach.)

In any case, using a typedef for a pointer type isn't nearly as bad as
using a #define for a pointer type.
 
C

Christian Christmann

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?

Regards,
Chris
 
C

Christian Christmann

Typedef's have a type thus the compiler can enforce typechecking.

But isn't typechecking also done with #defines?
I mean, before the compiler does its work, the #defines are already
substituted by the preprocessor, thus all mandatory information on
data types is available to the compiler. Or did I get something wrong?
Could you could post an example to make things clear.

Thank you.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top