Re: difference between typedef and #define
- From: "Drew Myers" <drew.nospam.myers@xxxxxxxx>
- Date: Thu, 30 Jun 2005 18:06:41 -0500
"Igor Tandetnik" <itandetnik@xxxxxxxx> wrote in message
news:OVPlyoPeFHA.640@xxxxxxxxxxxxxxxxxxxxxxx
> "Drew Myers" <drew.nospam.myers@xxxxxxxx> wrote in message
> news:%23MedTdPeFHA.2584@xxxxxxxxxxxxxxxxxxxx
>> Wouldn't a text substitution here create
>>
>> int* i,j,k;
>
> Yes
>
>> which would make i,j,k all int*s?
>
> No. To declare all three variables as int*, you have to write
>
> int *i, *j, *k;
>
But if I, without using typedef or #define write:
int* i,j,k; // * next to int and not next to i
all of the variables are int*, whereas:
int *i,j,k; // * next to i and not next to int
produces the results you explain. Why does typedef/#define
behave differently than typing it out as I did above?
Drew
> Blame Kernighan and Ritchie for the strange syntax of forming compound
> types in C. It gets better:
>
> int n, *p, a[10], f(long);
>
> This declares a variable n of type int, p of type int*, a of type int[10]
> (an array of 10 ints) and a function f taking long and returning an int.
>
> Because of these somewhat counterintuitive rules, it is usually a good
> idea to declare each variable separately.
>
>> It was my (limited) understanding
>> that the big
>> difference is that the typedef allows the ability to use 'a' in
>> templates and the #define does not.
>
> Since #define is just a textual substitution, it can be used anywhere a
> replacement text can appear. Consider
>
> #define a int*
> typedef int* b;
>
> template <typename T> void f();
> f<a>();
> f<b>();
> f<int*>(); // all three statements are equivalent
>
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>
.
- Follow-Ups:
- Re: difference between typedef and #define
- From: Barry Schwarz
- Re: difference between typedef and #define
- From: Doug Harrison [MVP]
- Re: difference between typedef and #define
- From: Victor Bazarov
- Re: difference between typedef and #define
- From: Robert Bunn
- Re: difference between typedef and #define
- Prev by Date: Basic Runtime Check stack issue
- Next by Date: IGlobalInterfaceTable (was: Debugging _com_ptr_t ... in JNI from J
- Previous by thread: Basic Runtime Check stack issue
- Next by thread: Re: difference between typedef and #define
- Index(es):
Relevant Pages
|