9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Axel Belinfante <Axel.Belinfante@cs.utwente.nl>
To: 9fans@cse.psu.edu
Subject: [9fans] C compiler question: initializer is not a constant
Date: Mon,  1 Oct 2001 15:31:49 +0200	[thread overview]
Message-ID: <200110011331.PAA05411@copernicus.cs.utwente.nl> (raw)

[-- Attachment #1: Type: text/plain, Size: 1256 bytes --]

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.

[-- Attachment #2: test.c --]
[-- Type: text/plain , Size: 175 bytes --]

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(){
}

             reply	other threads:[~2001-10-01 13:31 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-10-01 13:31 Axel Belinfante [this message]
2001-10-01 15:35 ` Douglas A. Gwyn
2001-10-01 16:20   ` Axel Belinfante
2001-10-02  8:25 ` Boyd Roberts
2001-10-01 13:35 David Gordon Hogan
2001-10-01 13:47 ` Axel Belinfante
2001-10-01 13:40 rob pike
2001-10-01 13:55 ` Axel Belinfante

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200110011331.PAA05411@copernicus.cs.utwente.nl \
    --to=axel.belinfante@cs.utwente.nl \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).