Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Few questions about casting away const. |
| Date | 2014-03-04 10:16 -0800 |
| Organization | None to speak of |
| Message-ID | <87lhwp25ih.fsf@smov.org> (permalink) |
| References | <lf4cq3$mie$1@speranza.aioe.org> |
Daniel <daniel@inventati.org> writes:
> const char *s = "abc";
> char *t = s;
>
> printf("%s", s);
>
> Firstly, the direct cast from 'const char *' to 'char *' which is seen
> on
> the second line. I've read that this is illegal in standard C, is that
> true?
As Ben said, yes, this is invalid (a constraint violation, requiring a
diagnostic), and there is no cast. There may or may not be an implicit
conversion, but since it's a constraint violation, the initialization
has no actual semantics, and a compiler is free to reject it.
> Secondly, given that printf's 's' specifier matches a 'char *', the
> comment in section 6.5.2.2p6 of C11 seems to indicate that these types
> are interchangeable in this situation. If the direct cast is invalid,
> would this be a legal alternative?:
>
>
> #define unconst(p) (unconst_(0, p))
>
> char *unconst_(int unused, ...)
> {
> va_list args;
> char *r;
>
> va_start(args, unused);
> r = va_arg(args, char *);
> va_end(args);
> return r;
> }
>
> const char *s = "abc";
> char *t = unconst(s);
Given your declarations, both of these are valid:
printf("%s", s);
printf("%s", t);
The standard doesn't say that "%s" requires a char* argument; it says
"the argument shall be a pointer to the initial element of an array of
character type", and both s and t qualify.
> Or, are there other possibilities?
What problem are you trying to solve?
If you want to convert a const char* to char*, just cast it:
const char *s = "abc";
char *t = (char*)s;
But consider carefully whether you need to do that. Creating a
non-const pointer to const data risks undefined behavior if you later
modify the pointed-to object.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Few questions about casting away const. Daniel <daniel@inventati.org> - 2014-03-04 11:18 +0000
Re: Few questions about casting away const. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-04 12:39 +0000
Re: Few questions about casting away const. Kaz Kylheku <kaz@kylheku.com> - 2014-03-04 17:05 +0000
Re: Few questions about casting away const. Keith Thompson <kst-u@mib.org> - 2014-03-04 10:16 -0800
Re: Few questions about casting away const. Daniel <daniel@inventati.org> - 2014-03-05 03:45 +0000
Re: Few questions about casting away const. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-05 04:20 +0000
Re: Few questions about casting away const. James Kuyper <jameskuyper@verizon.net> - 2014-03-04 15:18 -0500
Re: Few questions about casting away const. Daniel <daniel@inventati.org> - 2014-03-05 04:14 +0000
Re: Few questions about casting away const. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-05 04:35 +0000
Re: Few questions about casting away const. Daniel <daniel@inventati.org> - 2014-03-05 06:11 +0000
Re: Few questions about casting away const. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-05 12:14 +0000
Re: Few questions about casting away const. Daniel Harrison <daniel@inventati.org> - 2014-03-07 04:31 +0000
Re: Few questions about casting away const. Barry Schwarz <schwarzb@dqel.com> - 2014-03-06 21:23 -0800
Re: Few questions about casting away const. Kaz Kylheku <kaz@kylheku.com> - 2014-03-07 05:59 +0000
Re: Few questions about casting away const. James Kuyper <jameskuyper@verizon.net> - 2014-03-07 07:18 -0500
Re: Few questions about casting away const. Tim Rentsch <txr@alumni.caltech.edu> - 2014-03-10 00:30 -0700
Re: Few questions about casting away const. Keith Thompson <kst-u@mib.org> - 2014-03-10 09:00 -0700
Re: Few questions about casting away const. Tim Rentsch <txr@alumni.caltech.edu> - 2014-03-10 11:13 -0700
Re: Few questions about casting away const. Keith Thompson <kst-u@mib.org> - 2014-03-10 12:10 -0700
Re: Few questions about casting away const. Kaz Kylheku <kaz@kylheku.com> - 2014-03-11 07:38 +0000
Re: Few questions about casting away const. Tim Rentsch <txr@alumni.caltech.edu> - 2014-03-29 09:02 -0700
Re: Few questions about casting away const. Tim Rentsch <txr@alumni.caltech.edu> - 2014-03-10 02:29 -0700
Re: Few questions about casting away const. Keith Thompson <kst-u@mib.org> - 2014-03-10 10:52 -0700
Re: Few questions about casting away const. Tim Rentsch <txr@alumni.caltech.edu> - 2014-03-29 08:50 -0700
csiph-web