[Info-vax] C troubles: BADSTATICCVT
Craig A. Berry
craigberry at nospam.mac.com
Thu Feb 1 18:49:36 EST 2018
The following is a greatly-reduced example of some open source code that
compiles on many different platforms with many different compilers, but
not with the VMS compiler:
$ type static_const1.c
#include <inttypes.h>
struct data {
int stuff;
intptr_t nonsense;
};
static struct data mydata[] = {
42, (intptr_t)"."
};
With:
$ cc/version
VSI C V7.4-001 on OpenVMS IA64 V8.4-2L1
I get:
$ cc static_const1
42, (intptr_t)"."
..................^
%CC-E-BADSTATICCVT, In the initializer for mydata[0].nonsense, the
address cannot be converted to the destination type.
at line number 9 in file D0:[craig.TEST]static_const1.c;3
HELP CC MESSAGES BADSTATICCVT tells me, "A static initialization tried
to convert a link-time address to another type. However, the linker on
this platform will not support such a conversion." And also that I
should, "Rewrite the static initialization, or perform the
initialization using runtime code."
Hmm. Well, I tried various things that I thought might be construed as
rewriting the static initialization. The one that I thought made the
most sense was:
#include <inttypes.h>
struct data {
int stuff;
intptr_t nonsense;
};
const char* const dot = ".";
static struct data mydata[] = {
42, (intptr_t)dot
};
but that gives me:
$ cc static_const2
42, (intptr_t)dot
..................^
%CC-E-NEEDCONSTEXPR, In the initializer for mydata[0].nonsense, "dot" is
not constant, but occurs in a context that requires a constant expression.
at line number 11 in file D0:[craig.TEST]static_const2.c;17
So even though I said "const" (twice) in the declaration of dot and
initialized it to a literal constant, it still isn't constant enough.
Just among us ruffians, it kinda sorta makes sense that you cannot
create a compile-time constant containing the value of a link-time
address. Well, not without the ability to predict the future. But how
does every compiler on the market except the VMS compiler have that
magical capability? And what should I do that doesn't involve having to
understand everywhere that the struct being initialized is used?
More information about the Info-vax
mailing list