static int x[2], *p = x+(x-x); ?

Dave Hanson drh at cs.Princeton.EDU
Sun Sep 16 04:19:42 AEST 1990


In article <11907 at crdgw1.crd.ge.com> volpe at underdog.crd.ge.com (Christopher R Volpe) writes:
    In article <2699 at rossignol.Princeton.EDU    , drh at cs.Princeton.EDU (Dave
    Hanson) writes:
    	    static int *s = x + (x-x); /* Does this ``evaluate to'' x+0? */
    	    
    	    even though (x-x) is 0, compilers may not be obliged
    	    to recognize it as 0 because x-x doesn't conform to the allowable
    	    expressions you listed above.
    
    Yes, they are obliged to recognize it as zero because the two 
    operands (which happen to be identical) are both pointers into
    the same array. See K&R II A7.7

x-x indeed evaluates to 0 during execution, but compilers are not
obliged to recognize it as a constant expression that evaluates to 0
during compilation. x-x is not an integral constant expression because
its operands are address constants, not integer constants (see sec. 3.4).



More information about the Comp.std.c mailing list