char **(或char * [])是否可以隐式转换为'const char * const *'? [英] Is char** (or char*[]) implicitly convertible to 'const char * const *'?

查看:87
本文介绍了char **(或char * [])是否可以隐式转换为'const char * const *'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char **(或char * [])是否可以隐式转换为''const char * const

*''?


我无法'在标准中找不到任何关于它的东西。 MSVS 8.0允许

这个。我很好奇我是否会遇到其他编译器的问题,比如

GCC。


非常感谢!

- 凯文

解决方案

ke ******** @ motioneng.com 写道:

char **(或char * [])可以隐式转换为''const char * const
*''?


你把它放在括号中,就像它们是相同的类型一样。他们不是。

我在标准中找不到任何关于它的东西。 MSVS 8.0允许这样做。我很好奇我是否会遇到像GCC这样的其他编译器的麻烦。




答案是否定的,这些隐含的转换不是允许。需要

显式强制转换。来自常见问题:
http:/ /www.eskimo.com/~scs/C-faq/q11.10.html


当您使用gcc尝试此操作时,它会发出警告,无论

方言设置。其他编译器可能会出错。


S.


Skarmander,谢谢你的回复。我很感激。


我知道它们不是相同的类型 - 虽然char * []是隐含的

可转换为char **(但是显然不是相反的方式)。


我确实用Comeau的在线编译器验证C在严格模式下将不会允许转换(b)b非严格模式可以容忍

转换)。但是,Comeau的C ++编译器在严格模式下确实允许转换为



C ++在这方面真的更宽松吗? (不需要回复。我知道如果我想要一个答案,我必须首先搜索C ++

标准然后在comp.lang.c ++中发布如果我我输了。


不幸的是,C对此非常严格。我真的很努力在我的代码中使用
const-correct,如果我的函数将采用''char **''

参数但不修改它,那么使用

''const char * const *''来表示这一点似乎是谨慎的。但是期待其他人投票甚至更不可取。


有谁知道为什么C在这方面如此严格?这是什么东西

可以安全地不那么严格?


再次感谢!


- Kevin


2005-10-28,Skarmander< in ***** @ dontmailme.com>写道:

ke********@motioneng.com 写道:

char **(或char * [])是否可以隐式转换为''const char * const
*''?

你把它放在括号中就像他们是平等的类型。他们不是。




不,但是char * []可以隐式转换为char **。

< blockquote class =post_quotes>我在标准中找不到任何关于它的东西。 MSVS 8.0允许这样做。我很好奇我是否会遇到像GCC这样的其他编译器的麻烦。



答案是否定的,这些隐式转换是不允许的。需要进行明确的演员表演。来自常见问题:
http:/ /www.eskimo.com/~scs/C-faq/q11.10.html




我想知道的理由是什么这个。是否存在

是一个系统,其中const指针具有不同的表示形式

而不是非常数?


你(以及那个常见问题解答)似乎认为演员阵容会起作用,而且不会导致未定义的行为,这让我认为

约束是完全虚假的,并且只存在迫使人们为b $ b $类型不必要的演员阵容。如果有一个合法的情况是不允许

它隐式转换,那么演员也会导致未定义的

行为(比如试图将int **转换为void **)


另外,我确信理由文件中有一张表格

暗示这是允许的 - 也许是在POSIX中。 br />
当您使用gcc尝试此操作时,无论
方言设置如何,它都会发出警告。其他编译器可能会出错。

S。



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

解决方案

ke********@motioneng.com 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.


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


On 2005-10-28, Skarmander <in*****@dontmailme.com> wrote:

ke********@motioneng.com 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.



这篇关于char **(或char * [])是否可以隐式转换为'const char * const *'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆