Is #define THING -10 completely safe?

Dave P. Schaumann dave at cs.arizona.edu
Tue Jan 29 00:50:21 AEST 1991


In article <14999 at smoke.brl.mil> gwyn at smoke.brl.mil (Doug Gwyn) writes:
|In article <1991Jan27.233142.28302 at watdragon.waterloo.edu> ccplumb at rose.uwaterloo.ca (Colin Plumb) writes:
|-As all loyal followers of the Obfuscated C code contest know,
|-array[i] == *(array+i) == *(i+array) == i[array].  So
|-#define X1 -10
|-#define X2 (-10)
|-[...]
|-printf("%d, %d\n", X1[p], X2[p]);
|-Will print "-35, 15".
|
|No, that's a ludicrous misinterpretation of the situation.  The
|compiler does NOT perform a textual "rewrite" of the [] expression
|then reparse it.


This works as Colin says on my machine (compiling with gcc).  Here is the
full program:

#include <stdio.h>

#define X1  -10
#define X2 (-10)

main(void) {

  int i, a[50], *p = &a[25] ;

  for( i = 0 ; i < 50 ; i++ ) a[i] = i ;

  printf( "X1:%d X2:%d", X1[p], X2[p] ) ;

}

If Colin's interpretation is wrong, what *is* happening?

Dave Schaumann		|  And then -- what then?  Then, future...
dave at cs.arizona.edu	|  		-Weather Report



More information about the Comp.lang.c mailing list