Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is char** (or char*[]) implicitly convertible to 'const char * const *'?

Reply
Thread Tools

Is char** (or char*[]) implicitly convertible to 'const char * const *'?

 
 
kevin.hall@motioneng.com
Guest
Posts: n/a
 
      10-28-2005
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 compilers like
GCC though.

Many thanks!

- Kevin

 
Reply With Quote
 
 
 
 
Skarmander
Guest
Posts: n/a
 
      10-28-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Is char** (or char*[]) implicitly convertible to 'const char * const
> *'?
>


You put this in parentheses like they're equal types. They're not.

> 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 compilers like
> GCC though.
>


The answer is no, these implicit conversions are not allowed. An
explicit cast is required. From the FAQ:
http://www.eskimo.com/~scs/C-faq/q11.10.html.

When you try this with gcc, it will issue a warning regardless of
dialect settings. Other compilers might make it an error.

S.
 
Reply With Quote
 
 
 
 
kevin.hall@motioneng.com
Guest
Posts: n/a
 
      10-29-2005
Skarmander, thank you for your response. I do appreciate it.

I know they aren't equal types -- although char*[] is implicitly
convertible to char** (but obviously not the other way around).

I did verify with Comeau's online compiler that C in strict mode will
not allow the conversion (the non-strict mode is tolerant of the
conversion). However, Comeau's C++ compiler in strict mode does allow
the conversion.

Is C++ really more lenient in this respect? (no response required. I
know that if I want an answer I'll have to first search the C++
standard then post in comp.lang.c++ if I'm lost).

It's unfortunate that C is so strict about this. I really strive to be
const-correct in my code, and if my function is going to take 'char**'
argument but not modify it, then it seems prudent to indicate this with
'const char * const *'. But expecting other people to cast is even
more undesirable.

Does anyone know why C is so strict in this area? Is this something
that could be safely less-strict?

Thanks again!

- Kevin

 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      10-29-2005
On 2005-10-28, Skarmander <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
>> Is char** (or char*[]) implicitly convertible to 'const char * const
>> *'?
>>

>
> You put this in parentheses like they're equal types. They're not.


No, but char*[] is implicitly convertable to char **.

>> 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 compilers like
>> GCC though.
>>

>
> The answer is no, these implicit conversions are not allowed. An
> explicit cast is required. From the FAQ:
> http://www.eskimo.com/~scs/C-faq/q11.10.html.


I'd like to know the justification for this. Is there or might there
be a system on which const pointers have a different representation
than non-const ones?

You (and that FAQ) seem to think that a cast would work, and would
not cause undefined behavior, which makes me think that the
constraint is entirely spurious, and only exists to force people to
type needless casts. If there's a legitimate case for not allowing
it to implicitly convert, then a cast, too, would cause undefined
behavior (like trying to cast int ** to void **)

Also, i was certain there was a table in the rationale document that
implied this was allowed - maybe it was in POSIX.

> When you try this with gcc, it will issue a warning regardless of
> dialect settings. Other compilers might make it an error.
>
> S.

 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      10-29-2005
In article <(E-Mail Removed). com>,
<(E-Mail Removed)> wrote:
>Skarmander, thank you for your response. I do appreciate it.
>
>I know they aren't equal types -- although char*[] is implicitly
>convertible to char** (but obviously not the other way around).
>
>I did verify with Comeau's online compiler that C in strict mode will
>not allow the conversion (the non-strict mode is tolerant of the
>conversion). However, Comeau's C++ compiler in strict mode does allow
>the conversion.
>
>Is C++ really more lenient in this respect? (no response required. I
>know that if I want an answer I'll have to first search the C++
>standard then post in comp.lang.c++ if I'm lost).
>
>It's unfortunate that C is so strict about this. I really strive to be
>const-correct in my code, and if my function is going to take 'char**'
>argument but not modify it, then it seems prudent to indicate this with
>'const char * const *'. But expecting other people to cast is even
>more undesirable.
>
>Does anyone know why C is so strict in this area? Is this something
>that could be safely less-strict?


Can you post exactly the line of code you're saying Comeau C accepted
in strict mode but Comeau C++ rejected in strict mode? Thanks.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      10-29-2005
In article <(E-Mail Removed)>,
Jordan Abel <(E-Mail Removed)> wrote:
>On 2005-10-28, Skarmander <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>>> Is char** (or char*[]) implicitly convertible to 'const char * const
>>> *'?

>> You put this in parentheses like they're equal types. They're not.

>No, but char*[] is implicitly convertable to char **.
>
>>> 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 compilers like
>>> GCC though.

>>
>> The answer is no, these implicit conversions are not allowed. An
>> explicit cast is required. From the FAQ:
>> http://www.eskimo.com/~scs/C-faq/q11.10.html.

>
>I'd like to know the justification for this. Is there or might there
>be a system on which const pointers have a different representation
>than non-const ones?
>
>You (and that FAQ) seem to think that a cast would work, and would
>not cause undefined behavior, which makes me think that the
>constraint is entirely spurious, and only exists to force people to
>type needless casts. If there's a legitimate case for not allowing
>it to implicitly convert, then a cast, too, would cause undefined
>behavior (like trying to cast int ** to void **)
>
>Also, i was certain there was a table in the rationale document that
>implied this was allowed - maybe it was in POSIX.



Check out http://www.comeaucomputing.com/techtalk/#deconstutoh
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
 
Reply With Quote
 
kevin.hall@motioneng.com
Guest
Posts: n/a
 
      10-29-2005
It was the other way around. C++ strict mode accepted it. C strict
mode rejected it. Here's the code:

void test(const char * const * strArray)
{
}

int main()
{
char** x = 0;
test(x);
return 0;
}

 
Reply With Quote
 
kevin.hall@motioneng.com
Guest
Posts: n/a
 
      10-29-2005
Greg,

I understand why 'char**' is not convertible to 'const char**'. Here's
why "char**" is not convertible to "const char**". Here's how I
explain it to others. Let us see what could happen if the conversion
was allowed:

void foo(const char** strArray)
{
strArray[0] = "Hello"; // This is OK for const char**
}

void bar()
{
char s1[] = "This is"; // Creates an array of characters that are
initialized with the string "This is"
char s2[] = "a test"; // <same as above>
char*[] strArray = {s1, s2}; // Creates an array of pointers to
strings. Neither is constant.

foo(strArray); // Uh-oh strArray[0] now points to a constant
string, which I shouldn't be able to modify.

// According to the type of strArray, the following statement is
allowed.
// However, strArray[0] points to an area of constant memory (this
could in theory lie on a ROM chip)
strArray[0][0] = 'F'; // Ooops. Attempting to modify constant
memory. Undefined behavior ensues!!!
}



But I don't see where problems could pop up if 'char**' is converted to
'const char * const *' -- or am I missing something?

 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      10-29-2005
On 2005-10-29, Greg Comeau <(E-Mail Removed)> wrote:
> In article <(E-Mail Removed)>,
> Jordan Abel <(E-Mail Removed)> wrote:
>>On 2005-10-28, Skarmander <(E-Mail Removed)> wrote:
>>> (E-Mail Removed) wrote:
>>>> Is char** (or char*[]) implicitly convertible to 'const char * const
>>>> *'?
>>> You put this in parentheses like they're equal types. They're not.

>>No, but char*[] is implicitly convertable to char **.
>>
>>>> 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 compilers like
>>>> GCC though.
>>>
>>> The answer is no, these implicit conversions are not allowed. An
>>> explicit cast is required. From the FAQ:
>>> http://www.eskimo.com/~scs/C-faq/q11.10.html.

>>
>>I'd like to know the justification for this. Is there or might there
>>be a system on which const pointers have a different representation
>>than non-const ones?
>>
>>You (and that FAQ) seem to think that a cast would work, and would
>>not cause undefined behavior, which makes me think that the
>>constraint is entirely spurious, and only exists to force people to
>>type needless casts. If there's a legitimate case for not allowing
>>it to implicitly convert, then a cast, too, would cause undefined
>>behavior (like trying to cast int ** to void **)
>>
>>Also, i was certain there was a table in the rationale document that
>>implied this was allowed - maybe it was in POSIX.

>
>
> Check out http://www.comeaucomputing.com/techtalk/#deconstutoh


I don't get it - I mean, i _get_ it, but i don't see how you could
do this inadvertently. Forbidding something which doesn't actually
violate const rules just because a programmer could use it to make a
deliberate attempt to do so doesn't make sense.

Besides, if i look at the table, [found it in the posix exec()
manpage] which i misread the first time,

const char * const *ppcc = ppc;

which you suggest instead, is also forbidden.
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      10-29-2005
On 2005-10-29, (E-Mail Removed) <(E-Mail Removed)> wrote:
> Greg,
>
> I understand why 'char**' is not convertible to 'const char**'. Here's
> why "char**" is not convertible to "const char**". Here's how I
> explain it to others. Let us see what could happen if the conversion
> was allowed:
>
> void foo(const char** strArray)
> {
> strArray[0] = "Hello"; // This is OK for const char**
>}


Well, that's also ok for char**, since string literals are of type
char * in c. The general idea still stands, though.

The thing that irritates me is that despite all this, it's _trivial_
to violate const in C without resorting to all this.

const char foo[] = "mystring";
char *constviol = strchr(foo,*foo);
 
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 vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 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
Is char*[] convertible to const char*[]? kevin.hall@motioneng.com C Programming 10 09-05-2005 06:06 AM
extern const char * vs. extern const char []http://tinyurl.com/47e3k Thomas Matthews C++ 5 08-02-2004 10:36 AM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments