From mboxrd@z Thu Jan 1 00:00:00 1970 Message-Id: <200110011331.PAA05411@copernicus.cs.utwente.nl> To: 9fans@cse.psu.edu From: Axel Belinfante Mime-Version: 1.0 Content-Type: multipart/mixed ; boundary="==_Exmh_6098410700" Subject: [9fans] C compiler question: initializer is not a constant Date: Mon, 1 Oct 2001 15:31:49 +0200 Topicbox-Message-UUID: f8714704-eac9-11e9-9e20-41e7f4b1d025 This is a multipart MIME message. --==_Exmh_6098410700 Content-Type: text/plain; charset=us-ascii Last week I ported some code containing an initialization like one of h in attached test.c: struct { int a, b, c; } x; int h = (int)&x.b - (int)&x; The plan 9 C compilers (pcc -c, 8c) complain about it: test.c:8[stdin:9] initializer is not a constant: h The author of the code told me that it is accepted by ``all other C compilers except ...'' (and I think the exception named was the irix compiler). Are the plan 9 C compilers more strict here? In ``the C programming language'' (2nd edition) I found on page 209, near the end of A7.19: "Initializers must evaluate either to a constant or to the address of a previously declared external or static object plus or minus a constant." Is this the rule that applies? I tried some variants, like g: int g = (int)(&x.b - (int)&x); and the plan 9 C compilers also complain here (same error msg). However, gcc (2.95.3) and sun cc (Sun WorkShop 6 2000/04/07 C 5.1) do not complain about h, but they do complain about g: (gcc) /tmp/test.c:8: initializer element is not computable at load time (sun cc) "/tmp/test.c", line 8: an address is not allowed in a constant initializer for an integral type whose size is smaller than the size of a pointer Axel. --==_Exmh_6098410700 Content-Type: text/plain ; name="test.c"; charset=us-ascii Content-Description: test.c Content-Disposition: attachment; filename="test.c" struct { int a, b, c; } x; int d = (int)&x; int e = (int)&x.b; int f = (int)(&x.b - 1); int g = (int)(&x.b - (int)&x); int h = (int)&x.b - (int)&x; main(){ } --==_Exmh_6098410700--