Are typedef
's handled by C's preprocessor?
That is, is typedef int foo_t;
the same as #define foo_t int
?
No, because that type of replacement won't work with more complex types. For instance:
typedef int tenInts[10];
tenInts myIntArray;
The declaration of myIntArray
is equivalent to:
int myIntArray[10];
To do this as a macro you'd have to make it a function-style, so it can insert the variable name in the middle of the expansion.
#define tenInts(var) int var[10]
and use it like:
tenInts(myIntArray);
This also wouldn't work for declaring multiple variables. You'd have to write
tenInts(myArray1);
tenints(myArray2);
You can't write
tenInts myArray1, myArray2;
like you can with a typedef
.
No.
As an example,
typedef int *int_ptr1;
#define int_ptr2 int *
Then in:
int_ptr1 a, b;
int_ptr2 c, d;
a
and b
are both pointers to int
. c
is also a pointer to int
, but d
is an int
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With