Topic: Question about string literals
Author: "Neil M. Orme" <Broker@Exchangor.Com>
Date: 1999/03/22 Raw View
Re:
void *b = "Hello!"; // but is this?
My initial reaction is to typecast like this:
void *b = (void *)"Hello!";
A problem is that unit boundaries may be different,
but shouldn't hurt if address b is never changed.
I'm curious, what are you doing with this?
You can't print it or manipulate it easily - i.e., it's more thouble than
it's worth.
NEIL
Hyman Rosen <hymie@prolifics.com> wrote in message
news:36F15217.4DC798E2@prolifics.com...
> I know that in some cases, string literals can be used in places where
> (char *) rather than (const char *) is expected. I would like to know,
> and can't quite figure out from the Standard, how broad the permission
> is. Here's some code:
>
> char *a = "Hello!"; // This is definitely legal,
> void *b = "Hello!"; // but is this?
> void g(char *);
> void f(bool p)
> {
> g("Hello!"); // This is definitely legal,
> g(p ? "Hello!" : "Goodbye!"); // but is this?
> }
>
> For what it's worth, EGCS refuses to compile the questionable lines.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Hyman Rosen <hymie@prolifics.com>
Date: 1999/03/22 Raw View
"Neil M. Orme" wrote:
> void *b = "Hello!"; // but is this?
>
> My initial reaction is to typecast like this:
> void *b = (void *)"Hello!";
Well, yes, of course. But I wanted to know if the uncast usage was legal.
> A problem is that unit boundaries may be different,
> but shouldn't hurt if address b is never changed.
I don't understand what you mean.
> I'm curious, what are you doing with this?
> You can't print it or manipulate it easily - i.e., it's more thouble than
> it's worth.
This code is part of DDD. It builds up complicated static menu structures,
where there are members of type XtPointer (which is void *) initialized by
string literals. These XtPointers are used as callback parameters from X
toolkit event handlers and callbacks, hence the type. The code compiled
without error until recent versions of egcs. For what it's worth, I made
the appropriate patches and sent them along to the DDD maintainers, so
they will hopefully show up in a future version.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/03/24 Raw View
Neil M. Orme wrote:
> Re:
> void *b = "Hello!"; // but is this?
I guess that it is. ("Hello!" has type const char[],
which is converted to char[], then to void*.)
> My initial reaction is to typecast like this:
> void *b = (void *)"Hello!";
With such a cast, this is clearly valid.
> A problem is that unit boundaries may be different,
> but shouldn't hurt if address b is never changed.
???
> I'm curious, what are you doing with this?
> You can't print it or manipulate it easily - i.e., it's more thouble than
> it's worth.
He isn't doing that; he probably only wants to know if it's allowed
- we are in comp.STD.c++, after all.
--
Valentin Bonnard
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Hyman Rosen <hymie@prolifics.com>
Date: 1999/03/19 Raw View
I know that in some cases, string literals can be used in places where
(char *) rather than (const char *) is expected. I would like to know,
and can't quite figure out from the Standard, how broad the permission
is. Here's some code:
char *a = "Hello!"; // This is definitely legal,
void *b = "Hello!"; // but is this?
void g(char *);
void f(bool p)
{
g("Hello!"); // This is definitely legal,
g(p ? "Hello!" : "Goodbye!"); // but is this?
}
For what it's worth, EGCS refuses to compile the questionable lines.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1999/03/19 Raw View
On Mar 19, 1999, Hyman Rosen <hymie@prolifics.com> wrote:
> I know that in some cases, string literals can be used in places where
> (char *) rather than (const char *) is expected. I would like to know,
> and can't quite figure out from the Standard, how broad the permission
> is.
There's a deprecated implicit conversion from string literal to
char* [conv.array]/2, but that's all. (a?"b":"c") is not a string
literal, therefore it can't be converted to char* [diff.lex]/4
--
Alexandre Oliva http://www.dcc.unicamp.br/~oliva IC-Unicamp, Brazil
{oliva,Alexandre.Oliva}@dcc.unicamp.br aoliva@{acm.org,computer.org}
oliva@{gnu.org,kaffe.org,egcs.cygnus.com,samba.org}
*** E-mail about software projects will be forwarded to mailing lists
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]