为什么不能确定通过`焦炭**`给带有`为const char功能**`用C? [英] Why is it not OK to pass `char **` to a function that takes a `const char **` in C?
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/78125/why-cant-i-convert-char-to-a-const-char-const-in-c\">Why能和rsquo的; T I转换&lsquo的;字符*的&rsquo的;一个&lsquo的;为const char 的常量* rsquo的;用C?
Possible Duplicate:
Why can’t I convert ‘char*’ to a ‘const char const*’ in C?
我很好奇,为什么我不能传递的char **
到为const char **
功能?在这里,因为它是确定的char *
传递给为const char *
功能它似乎不是好做双指针。我认为这是永远OK添加常量性(但不正常下降常量性),但现在看来我错了。
I am curious, why can't I pass a char **
to const char **
function? Where as it is OK to pass char *
to a const char *
function it seems not to be OK to do it with double pointers. I thought it was always ok to add constness (but not ok to drop constness) but now it seems I have been wrong.
gcc编译是给我的errror:
Gcc compiler is giving me the errror:
note: expected ‘const char **’ but argument is of type ‘char **’
下面是code片断:
int f(const char **a) { }
int main() {
char *a;
f(&a);
}
任何想法?
推荐答案
由于编译器不能保证安全。
Because the compiler can't guarantee safety.
从comp.lang.c常见问题见Q11.10:为什么我不能传递字符**
来一个希望将功能的为const char **
?
See Q11.10 from the comp.lang.c FAQ: Why can't I pass a char **
to a function which expects a const char **
?
这篇关于为什么不能确定通过`焦炭**`给带有`为const char功能**`用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!