Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are typedef's handled by the preprocessor? [duplicate]

Are typedef's handled by C's preprocessor?

That is, is typedef int foo_t; the same as #define foo_t int?


2 Answers

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.

like image 107
Barmar Avatar answered Sep 21 '25 07:09

Barmar


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.

like image 29
Yu Hao Avatar answered Sep 21 '25 06:09

Yu Hao