hartzell@boulder.colorado.edu (George Hartzell) (06/06/90)
The following test program compiles and runs on suns (SUNos 4.0x,
sun4), but will not compile on a MIPS with the 2.0 compilers or a
DECstation with the 1.31 compiler suite. There error message is (this
is from the DECstation)
ccom: Error: moose.c, line 8: constant expected
static int ia[] = { ((char *) &X.a - (char *) &X), ((char *)
X.b - (cha
r *) &X), ((char *) &X.c - (char *) &X) } ;
--------------------------------------------------^
ccom: Error: moose.c, line 8: illegal initialization
static int ia[] = { ((char *) &X.a - (char *) &X), ((char *)
X.b - (cha
r *) &X), ((char *) &X.c - (char *) &X) } ;
--------------------------------------------------^
This also works on gcc for the DECstation...
I have two questions:
a) should this be legal [in the ansi sense?, in the practical
sense?] (The compiler should have all the info it needs to
figure this out, shouldn't it?)
b) do the 2.10 compilers support this construction?
g.
-----cut here for sample program --------------------------------------------
#define CharAttI(a) ((char *) X.a - (char *) &X)
#define AttIndex(a) ((char *) &X.a - (char *) &X)
typedef struct { int a; char b[8]; int c; } Thing;
static Thing X;
static int ia[] = { AttIndex(a), CharAttI(b), AttIndex(c) } ;
main()
{
printf( "\nia: %d %d %d\n", ia[0], ia[1], ia[2] ) ;
}
George Hartzell (303) 492-4535
MCD Biology, University of Colorado-Boulder, Boulder, CO 80309
hartzell@Boulder.Colorado.EDU ..!ncar!boulder!hartzell
diamond@tkou02.enet.dec.com (diamond@tkovoa) (06/06/90)
In article <21901@boulder.Colorado.EDU> hartzell@beagle (George Hartzell) writes: > ccom: Error: moose.c, line 8: constant expected > static int ia[] = { ((char *) &X.a - (char *) &X), ((char *) > X.b - (cha > r *) &X), ((char *) &X.c - (char *) &X) } ; > --------------------------------------------------^ >I have two questions: > a) should this be legal [in the ansi sense?, in the practical > sense?] (The compiler should have all the info it needs to > figure this out, shouldn't it?) > b) do the 2.10 compilers support this construction? Well, here are your two answers: a) no, it's subjective, and yes. b) I don't know. but who's counting? It is not legal in ANSI. Whether it should be accepted practically is a matter of opinion. I personally think that it should be accepted with an optional warning. I think that the compiler has enough information to figure it out. Other compilers certainly prove that it is possible. I have seen rumors that MIPS, and perhaps DEC, are aware that competitors can handle this case. But if I were privileged enough to know their plans, I would not be allowed to tell you. -- Norman Diamond, Nihon DEC diamond@tkou02.enet.dec.com Proposed group comp.networks.load-reduction: send your "yes" vote to /dev/null.