Mixed mode Arithmatic
Peter Jaspers-Fayer
SOFPJF at VM.UOGUELPH.CA
Fri Feb 22 02:44:18 AEST 1991
Seems that SGI C is different from any other C that we've seen when it
comes to adding pointers and long ints in various combinations.
#include <stdio.h>
main(){
long a, b, *P;
char s[2000];
a = 2;
b = 5;
P = (long*)s;
printf("Sizes: a P %d %d\n",sizeof(a),sizeof(P));
printf("s,P: %ld %ld\n", s,P);
printf("a,b: %ld %ld\n", a,b);
printf("a+P+b: %ld\n", a+P+b);
printf("a+b+P: %ld\n", a+b+P);
printf("a+(P+b): %ld\n", a+(P+b));
printf("a+(b+P): %ld\n", a+(b+P)); /* Watch this one */
}
Sequent and others:
Sizes: a P 4 4
s,P: 1073735252 1073735252
a,b: 2 5
a+P+b: 1073735280
a+b+P: 1073735280
a+(P+b): 1073735280
a+(b+P): 1073735280
SGI PI:
Sizes: a P 4 4
s,P: 2147467180 2147467180
a,b: 2 5
a+P+b: 2147467208
a+b+P: 2147467208
a+(P+b): 2147467208
a+(b+P): 2147467292 [!]
As there does not appear to be a fixed standard, I can't say who's
"right", but this note will (I hope) serve as a warning to others that
may be surprised at how litterally SGI's C takes these sort of
expressions.
In every other compiler, I would guess that the ints are added together
first, then the result used to bump the pointer, regardless of the order
or punctuation of the expression.
/PJ SofPJF at VM.UoGuelph.Ca
--------------------------------------
An ounce of application is worth a ton of abstraction.
More information about the Comp.sys.sgi
mailing list