9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] C compiler question: initializer is not a constant
@ 2001-10-01 13:31 Axel Belinfante
  2001-10-01 15:35 ` Douglas A. Gwyn
  2001-10-02  8:25 ` Boyd Roberts
  0 siblings, 2 replies; 8+ messages in thread
From: Axel Belinfante @ 2001-10-01 13:31 UTC (permalink / raw)
  To: 9fans

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

^ permalink raw reply	[flat|nested] 8+ messages in thread
* Re: [9fans] C compiler question: initializer is not a constant
@ 2001-10-01 13:35 David Gordon Hogan
  2001-10-01 13:47 ` Axel Belinfante
  0 siblings, 1 reply; 8+ messages in thread
From: David Gordon Hogan @ 2001-10-01 13:35 UTC (permalink / raw)
  To: 9fans

> 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).

The author of the code needs to be visitted by the Style Police.
Jeez!



^ permalink raw reply	[flat|nested] 8+ messages in thread
* Re: [9fans] C compiler question: initializer is not a constant
@ 2001-10-01 13:40 rob pike
  2001-10-01 13:55 ` Axel Belinfante
  0 siblings, 1 reply; 8+ messages in thread
From: rob pike @ 2001-10-01 13:40 UTC (permalink / raw)
  To: 9fans

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

The compiler is justified in complaining, since it doesn't know the
address of x and can't be expected to recognize that it doesn't need
to know it. If you insist on such horrors, give it the address. The
clean way is to use the ANSI offsetof macro, but since the author
clearly doesn't care about cleanliness, just write something like

	int h = (int) &(((struct x*)0)->b);

-rob


[-- Attachment #2: Type: message/rfc822, Size: 4008 bytes --]

[-- Attachment #2.1.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.1.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(){
}

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2001-10-02  8:25 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-01 13:31 [9fans] C compiler question: initializer is not a constant Axel Belinfante
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

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).