should 'float* foo = 12345' be legal?

J

John

6.3.2.3 seems to say "yes", but 6.5.16.1 seems to say "no".

Reason I'm asking is that the Ogg Vorbis source uses this pattern, and
although it compiles fine on MSC and gcc 4, it does not compile on
Freescale's C compiler - so my question is, who's right?

Thanks.
 
B

Ben Pfaff

John said:
Subject: should 'float* foo = 12345' be legal?

6.3.2.3 seems to say "yes", but 6.5.16.1 seems to say "no".

6.3.2.3 says that an integer may be converted to a pointer type.
That's fine.

6.5.16.1 talks about assignment. It also applies to the
initializer you're discussing because of 6.7.8p11. The
code you're talking about does not meet the constraints in that
section, so it isn't compliant.
Reason I'm asking is that the Ogg Vorbis source uses this pattern, and
although it compiles fine on MSC and gcc 4, it does not compile on
Freescale's C compiler - so my question is, who's right?

Any compliant compiler must issue a diagnostic, because there is
a constraint violation. The compiler can then choose to produce
output or to abort, at its option--the standard doesn't say what
should happen.
 
R

Random832

2006-11-17 said:
6.3.2.3 seems to say "yes", but 6.5.16.1 seems to say "no".

Reason I'm asking is that the Ogg Vorbis source uses this pattern,

What's the actual line?
 
E

Eric Sosman

(For future reference: It's better to put your question
in the body of your message rather than just in the Subject
line. The display of the Subject is pretty much at the
whim of the news client or mail reader, and is sometimes
at a considerable distance from the message body -- maybe
in a different window, for example, possibly obscured from
view by the message window. For those who may wonder what
John asked, it was "should `float* foo = 12345' be legal?").

John wrote On 11/17/06 11:54,:
6.3.2.3 seems to say "yes", but 6.5.16.1 seems to say "no".

6.3.2.3 says that it is possible to make the conversion,
but not that the conversion is automatic. It is also possible
to convert "pointer to struct" to "pointer to initial element,"
but even though the conversion is permitted it doesn't mean it
"just happens."
Reason I'm asking is that the Ogg Vorbis source uses this pattern, and
although it compiles fine on MSC and gcc 4, it does not compile on
Freescale's C compiler - so my question is, who's right?

A diagnostic is required, because 6.7.8/11 says that the
initialization of a scalar is subject to the same constraints
as simple assignment, and the code at hand does not meet the
constraints of 6.5.16.1/1. Having issued a diagnostic, the
compiler is then free to behave as it likes; it may even
"compile" the diagnosable code into something useful. Then
again, it may not.

(See your gcc documentation for information about how to
get it to compile Standard C instead of "gcc C," and how to
enable its copious and helpful warnings. I'm not acquainted
with the other compilers you mention; maybe they, too, can be
made to operate in less or more loosey-goosey modes.)

If you've really no alternative except to use the vile
code pretty much as it stands, you can try

float* foo = (float*)12345;

.... despite the caveats of 6.3.2.3/5.
 
K

Keith Thompson

6.3.2.3 seems to say "yes", but 6.5.16.1 seems to say "no".

6.3.2.3 says that an integer may be converted to a pointer type. It
doesn't say that such a conversion will be performed implicitly, and
in fact it won't (unless your compiler offers such an implicit
conversion as an extension).
Reason I'm asking is that the Ogg Vorbis source uses this pattern, and
although it compiles fine on MSC and gcc 4, it does not compile on
Freescale's C compiler - so my question is, who's right?

Frankly, I'd be astonished if the Ogg Vorbis source actually had a
statement like that. I'm not familiar with it, but I believe it's
supposed to be reasonable portable. For a declaration

float *foo = 12345;

to make sense, there would have to be an object of type float at that
specific address; I can't imagine why the code would make such an
assumption.

Can you show us the *actual* code you're talking about?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top