Topic: Literal string assignment


Author: AllanW@my-dejanews.com
Date: 1999/03/09
Raw View
In article <CxKFgVANeS42EwDG@robinton.demon.co.uk>,
  Francis Glassborow <francisG@robinton.demon.co.uk> wrote:
>
> In article <36E07D51.45264A14@abraxis.com>, Edward Diener
> <eddielee@abraxis.com> writes
> >The declaration
> >
> >const unsigned char * x = "anything";
> >
> >gives an error in a C++ compiler that I use. Is it really not possible
> >to assign a literal string to a pointer to a const unsigned char as
> >opposed to a pointer to a signed char ?
>
> My guess is that it depends on how plain char is being implemented.
> Actually, when I stop to think about it, that should be irrelevant.
> "anything" has type char const and in this context that decays to
> char const *.  The C++ rules forbid implicit conversion of pointers
> (other than to void *)

In C++ there are three types of pointers to char, ignoring const and
volatile:
    signed char*
    unsigned char*
    char* // Unspecified
The last type might be either signed or unsigned, depending on the
compiler, but either way it is a distinct type.

String literals decay to const char*'s. To assign them to either a
const signed char* or const unsigned char*, you must use some form
of cast. (To assign them to a non-const char* of any type, you must
use a const_cast or an old C-style cast, both of which are dangerous.)

----
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ 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: Edward Diener <eddielee@abraxis.com>
Date: 1999/03/06
Raw View
The declaration

const unsigned char * x = "anything";

gives an error in a C++ compiler that I use. Is it really not possible
to assign a literal string to a pointer to a const unsigned char as
opposed to a pointer to a signed char ?
---
[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/03/06
Raw View
In article <36E07D51.45264A14@abraxis.com>, Edward Diener
<eddielee@abraxis.com> writes
>The declaration
>
>const unsigned char * x = "anything";
>
>gives an error in a C++ compiler that I use. Is it really not possible
>to assign a literal string to a pointer to a const unsigned char as
>opposed to a pointer to a signed char ?

My guess is that it depends on how plain char is being implemented.
Actually, when I stop to think about it, that should be irrelevant.
"anything" has type char const and in this context that decays to
char const *.  The C++ rules forbid implicit conversion of pointers
(other than to void *)


Francis Glassborow      Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ 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: Stephen.Clamage@sun.com (Steve Clamage)
Date: 1999/03/06
Raw View
Edward Diener <eddielee@abraxis.com> writes:

>The declaration

>const unsigned char * x = "anything";

>gives an error in a C++ compiler that I use. Is it really not possible
>to assign a literal string to a pointer to a const unsigned char as
>opposed to a pointer to a signed char ?

C++ has three char types: "plain" char, signed char, and unsigned
char.  These types are all different. A literal string has type
"array of plain char". In an expresion, that decays to "pointer
to plain char". There is no implicit conversion among pointers
to different types (except from derived class to base class).

It is true that plain char must be implemented the same as either
signed or unsigned char, but that doesn't mean that the types
are the same. For example, on many systems type int has the
same implementation as type long, but there is no automatic
conversion between int* and long*.

Using other than plain char to represent character data is
usually not a good idea, because you generally run afoul of type
mismatches. Library functions dealing with arrays of and pointers
to char expect plain char, in particular.

--
Steve Clamage, stephen.clamage@sun.com


[ 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: John Aldridge <jpsa@jjdash.demon.co.uk>
Date: 1999/03/06
Raw View
In article <36E07D51.45264A14@abraxis.com>, Edward Diener
<eddielee@abraxis.com> writes
>The declaration
>
>const unsigned char * x = "anything";
>
>gives an error in a C++ compiler that I use. Is it really not possible
>to assign a literal string to a pointer to a const unsigned char as
>opposed to a pointer to a signed char ?

Neither is legal.  In the following:

    const unsigned char *a = "a";
    const signed char *b = "b";
    const char *c = "c";

Only the last line is acceptable.  "char", "signed char" and "unsigned
char" are all distinct types.  This is true in C as well as C++.

--
Cheers,
John
---
[ 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              ]