By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,144 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,144 IT Pros & Developers. It's quick & easy.

should 'float* foo = 12345' be legal?

P: n/a
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.
Nov 17 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
John <jo**@x.yywrites:
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.
--
"Some people *are* arrogant, and others read the FAQ."
--Chris Dollin
Nov 17 '06 #2

P: n/a
2006-11-17 <2P*******************@tornado.texas.rr.com>,
John wrote:
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?
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.
Nov 17 '06 #3

P: n/a

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

--
Er*********@sun.com

Nov 17 '06 #4

P: n/a
John <jo**@x.yywrites:
[ 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. 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?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.