Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > const char * and char * are compatible pointer types?

Reply
Thread Tools

const char * and char * are compatible pointer types?

 
 
max
Guest
Posts: n/a
 
      01-05-2006
Dear all,

I did the following analysis to conclude that the following pointer
types are not compatible. Please let me know If my analysis and
interpretation of the C standard are correct:

const char * : "pointer to const-qualified char".
char *: "pointer to char".

Are these pointed-to types compatibles?

--------------------------
C89
--------------------------
6.5.4.1
Pointer declarators
....
For two pointer types to be compatible, both shall be identically
qualified
and both shall be pointers to compatible types.
....
--------------------------

Well, using definition in 6.5.4.1 we can see that we comply with the
first part
("both shall be identically qualified").
Both are identically qualified (no qualifiers, both are unqualified
types).

Let us see now about the second part of definition in 6.5.4.1
("and both shall be pointers to compatible types").

So do these pointers:

1) const char *; /* pointer to const-qualified char */
2) char *; /* pointer to char */

point to compatible types?

In the case of pointer 1) const char *;
this pointer points to a 'const-qualified char'.
In the case of pointer 2) char *;
this pointer points to a 'char'.

Is a 'const-qualified char' compatible with a
'char'?

--------------------------
C89
--------------------------
6.1.2.5
Types
.....
The qualified or unqualified versions of a type are distinct types that
belong to the same type category and have the same representation and
alignment requirements.
....
--------------------------

As we can see according to 6.1.2.5 and
6.1.2.6 ("Two types have compatible type if their types are the same").
A 'const-qualified char' is NOT compatible with a 'char'
because their types are different.

And as we can see:

1) const char *; /* pointer to const-qualified char */
2) char *; /* pointer to char */

cannot satisfy second part of definition in
6.5.4.1 ("and both shall be pointers to compatible types"). Because
they do not point to compatible types.

Therefore 1) and 2) are NOT compatible pointers.

Thank you very much to everybody in advance,

Max

 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      01-05-2006

"max" <(E-Mail Removed)> wrote
> const char * : "pointer to const-qualified char".
> char *: "pointer to char".
>
> Are these pointed-to types compatibles?
>

The technical answer is no. A const char * cannot be converted to a plain
char *, whilst a char * can be converted to a const char *.
The reason is that const char * points to strings that may not be modified.
You can treat a mutable string as immodifiable, but not a string in
read-only memory as modifiable.

Unfortunately const was a late addition to the language. As a result, a lot
of C code uses plain char *s where const char * would have been more
appropriate. To avoid breaking old code, the conversion between char * and
const char *was made rather loose.

(There are also problems with functions like strchr(). It returns a pointer
to the first instance of a character in a string. So if you pass it a
mutable string you want a char *, if you pass it a read-only string you want
a const char * back. However there is no easy way of doing this in the
language as it stands. Hence the function returns a char *.)



 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-05-2006
"Malcolm" <(E-Mail Removed)> writes:
> "max" <(E-Mail Removed)> wrote
>> const char * : "pointer to const-qualified char".
>> char *: "pointer to char".
>>
>> Are these pointed-to types compatibles?
>>

> The technical answer is no. A const char * cannot be converted to a plain
> char *, whilst a char * can be converted to a const char *.
> The reason is that const char * points to strings that may not be modified.
> You can treat a mutable string as immodifiable, but not a string in
> read-only memory as modifiable.


A char* or const char* doesn't necessarily point to a string. If it's
valid and non-null, it points to a character; that character may or
may not be the first element of an array (or, arguably, it's always
the first element of an array, possibly of length 1), and the array
may or may not be a string.

I think you're thinking of the issue of string literals (for which the
corresponding character array isn't const, but trying to modify it
invokes undefined behavior) -- but the original question didn't
mention string literals. Strings and string literals certainly
motivate the rules, but they aren't necessary to describing them.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      01-05-2006
On 2006-01-05, Malcolm <(E-Mail Removed)> wrote:
>
> "max" <(E-Mail Removed)> wrote
>> const char * : "pointer to const-qualified char".
>> char *: "pointer to char".
>>
>> Are these pointed-to types compatibles?
>>

> The technical answer is no. A const char * cannot be converted to a plain
> char *, whilst a char * can be converted to a const char *.


However, a const char * can be _cast_ to a plain char *, and will not
break unexpectedly. That is, if the const char * value was originally
converted from a modifiable char * in the first place, of if the object
pointed to by the resulting char * pointer is not in fact modified,
there is no undefined behavior invoked.

> The reason is that const char * points to strings that may not be modified.
> You can treat a mutable string as immodifiable, but not a string in
> read-only memory as modifiable.
>
> Unfortunately const was a late addition to the language. As a result, a lot
> of C code uses plain char *s where const char * would have been more
> appropriate. To avoid breaking old code, the conversion between char * and
> const char *was made rather loose.


Actually, the only concession made to such issues was that string
literals, though immutable, are of type char *. This can be changed in
some modern compilers, and there is some debate as to whether this
actually causes such implementations to be non-conforming.
 
Reply With Quote
 
Douglas A. Gwyn
Guest
Posts: n/a
 
      01-06-2006
Jordan Abel wrote:
> Actually, the only concession made to such issues was that string
> literals, though immutable, are of type char *. This can be changed in
> some modern compilers, and there is some debate as to whether this
> actually causes such implementations to be non-conforming.


There shouldn't be any question about it:
char *s; // not const-qualified
s = "Am I const?"; // must be allowed
If the compiler supports the operation, but also issues a
diagnostic, then technically it might be conforming but in
practice it will be a pain.
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      01-06-2006

"Jordan Abel" <(E-Mail Removed)> wrote
> Actually, the only concession made to such issues was that string
> literals, though immutable, are of type char *. This can be changed in
> some modern compilers, and there is some debate as to whether this
> actually causes such implementations to be non-conforming.
>

Also strchr(). Also the fact that a cast from a const char * to a char * is
legal, which ideally it shouldn't be.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const pointer versus const content pointed to by pointer Disc Magnet C Programming 1 05-06-2010 10:27 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Smart Pointer release() const : it can set the pointer to null with the keyword "const"? coala C++ 1 09-06-2006 03:00 PM
Smart Pointer release() const : it can set the pointer to null with the keyword "const"? coala C++ 3 09-06-2006 02:58 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM



Advertisments