#define v typedef

Hi! I was reading some questions on a tutorial site and admin wrote this code... Can anyone explain me pls why is pW just int on the second case??

1
2
3
4
5
 typedef int* intptr;
intptr pX, pY; // both pX and pY are int*
 
#define int* intptrdef;
intptrdef pZ, pW; // pZ is int*, but pW is just int! 
Because C is ridiculous with how it does pointer definitions.

#define macros are merely text substitution. So in your second case...
1
2
3
4
5
6
7
8
9
10
11
// this:
intptrdef pZ, pW;

// gets replaced with this:
int* pZ, pW;

// which, because C is ridiculous with how it does pointer definitions is closer to this:
int *pZ, pW;

// whereas if you wanted both of them to be pointers, you'd do this:
int *pZ, *pW;
ok, know I understand that! thanks a lot!
Topic archived. No new replies allowed.