473,385 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

const char * and char * are compatible pointer types?

max
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

Jan 5 '06 #1
5 5309

"max" <ma*****@yahoo.com> 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 *.)

Jan 5 '06 #2
"Malcolm" <re*******@btinternet.com> writes:
"max" <ma*****@yahoo.com> 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) 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.
Jan 5 '06 #3
On 2006-01-05, Malcolm <re*******@btinternet.com> wrote:

"max" <ma*****@yahoo.com> 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.
Jan 5 '06 #4

"Jordan Abel" <ra*******@gmail.com> 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.
Jan 6 '06 #5
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.
Jan 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: george doubleu | last post by:
hi, i'm using "gcc version 3.3.3 (Debian)" to compile the program pasted below. I get the two warnings you can see in the remarks. The second warning is perfectly OK for me, but the first one I...
11
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
8
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is transparently made const when I pass it to the function, e.g....
24
by: kevin.hall | last post by:
Is char** (or char*) implicitly convertible to 'const char * const *'? I couldn't find anything about it in the standard. MSVS 8.0 allows this. I'm curious if I'll run into trouble with other...
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
6
by: subramanian | last post by:
Consider the following program: #include <stdio.h> void myfn(const int **a) { static int i, j, k; a = &i; a = &j;
14
by: Jonas.Holmsten | last post by:
Hello I'm porting some C++ stuff to C and having problem to get it through gcc. Here is a condensed version of the problem: void foo(const int * const * const ptr) {} main()
4
by: lovecreatesbea... | last post by:
Gcc only gives out a warning: `assignment discards qualifiers from pointer target type' against code such as following: $ type a.c int main(void) { const char *pc; char *p = pc;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.