将字符串常量赋给char * [英] assigning string constant to char *
问题描述
以下第一个函数编译,第二个给出了什么
我认为是一个虚假的错误:
无法转换`const char [ 5]''to char *''in assignment"
void foo(int m){
char * str;
if(m< 4)str ="%01x";
else if(m< 9)str ="%02x";
else str ="%03x";
}
void foo(int m){
char * str;
str = m< 4? "%01X" :m< 9? "%02X" :%03x;
}
任何想法为什么gcc不喜欢第二个版本?如果我编译
它作为C文件而不是C ++,它工作正常。
Steve
The first of the following functions compiles, the second gives what
I think is a spurious error:
"cannot convert `const char[5]'' to `char *'' in assignment".
void foo(int m) {
char *str;
if (m < 4) str = "%01x";
else if (m < 9) str = "%02x";
else str = "%03x";
}
void foo(int m) {
char *str;
str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
}
Any idea why gcc doesn''t like the second version? If I compile
it as a C file instead of C++, it works fine.
Steve
推荐答案
Steve Pope写道:
Steve Pope wrote:
以下第一个函数编译,第二个给出了什么
我认为是一个虚假的错误:
无法在赋值中将`const char [5]''转换为`char *''。
void foo(int m){
char * str;
if(m< 4)str ="%01x";
否则if(m <9)str ="%02x";
else str ="%03x";
}
>
void foo(int m){
char * str;
str = m< 4? "%01X" :m< 9? "%02X" :%03x;
}
任何想法为什么gcc不喜欢第二个版本?如果我将它编译成一个C文件而不是C ++,它可以正常工作。
The first of the following functions compiles, the second gives what
I think is a spurious error:
"cannot convert `const char[5]'' to `char *'' in assignment".
void foo(int m) {
char *str;
if (m < 4) str = "%01x";
else if (m < 9) str = "%02x";
else str = "%03x";
}
void foo(int m) {
char *str;
str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
}
Any idea why gcc doesn''t like the second version? If I compile
it as a C file instead of C++, it works fine.
这不是一个虚假的错误,只是一个不起眼的错误。字符串常量
(如%01x)实际上是const char [5]类型,它衰减为const
char *。由于历史原因(为了不打破很多现有的
代码),你可以为非const char *分配一个字符串常量,尽管
它会弯曲(如果不打破)const-correctness。在第一个例子中,你是
正在为char *分配一个字符串常量,并且这是允许的。在
第二个例子中,你不是指定一个字符串常量,而是指定一个
表达式给char *。这是不允许的 - 例外
到const-correctness有意保持狭窄。
要解决您的问题,请将您的第二个示例更改为以下内容:
void foo(int m){
const char * str = m< 4? "%01X" :m< 9? "%02X" :"%03x"
}
因为你现在要分配给一个const char *,而不是一个非const char *,
问题消失了。
祝你好运,
Tom
It is not a spurious error, just an obscure one. A string constant
(like "%01x") is actually of type const char[5], which decays to const
char*. For historical reasons (so as not to break a lot of existing
code), you can assign a string constant to a non-const char*, although
it bends (if not breaks) const-correctness. In the first example, you
are assigning a string constant to a char*, and that''s permitted. In
the second example, you are assigning not a string constant, but an
expression instead, to the char*. That''s not permitted - the exception
to const-correctness is intentionally kept narrow.
To solve your problem change your second example to the following:
void foo(int m) {
const char *str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
}
Since you''re now assigning to a const char*, not to a non-const char*,
the problem disappears.
Best regards,
Tom
Thomas Tutone< Th *********** @ yahoo.comwrote:
Thomas Tutone <Th***********@yahoo.comwrote:
> Steve Pope写道:
>Steve Pope wrote:
> void foo(int m){
char * str;
str = m< 4? "%01X" :m< 9? "%02X" :"%03x" ;;
}
>void foo(int m) {
char *str;
str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
}
>任何想法为什么gcc不喜欢第二个版本?如果我将它编译为C文件而不是C ++,它可以正常工作。
>Any idea why gcc doesn''t like the second version? If I compile
it as a C file instead of C++, it works fine.
>这不是一个虚假的错误,只是一个不起眼的错误。字符串常量
(如%01x)实际上是const char [5]类型,它衰减为const
char *。由于历史原因(为了不打破很多现有的代码),你可以为非const char *分配一个字符串常量,尽管它会弯曲(如果没有中断)const-correctness。在第一个例子中,你正在为char *分配一个字符串常量,并且这是允许的。在第二个例子中,你不是为char *指定一个字符串常量,而是指定
表达式。这是不允许的 - 对于const-correctness的例外有意保持狭窄。
>It is not a spurious error, just an obscure one. A string constant
(like "%01x") is actually of type const char[5], which decays to const
char*. For historical reasons (so as not to break a lot of existing
code), you can assign a string constant to a non-const char*, although
it bends (if not breaks) const-correctness. In the first example, you
are assigning a string constant to a char*, and that''s permitted. In
the second example, you are assigning not a string constant, but an
expression instead, to the char*. That''s not permitted - the exception
to const-correctness is intentionally kept narrow.
好的,这是有道理的。
Okay, that makes sense.
>要解决您的问题,请将您的第二个示例更改为以下:
>To solve your problem change your second example to the following:
> void foo(int m){
const char * str = m< 4? "%01X" :m< 9? "%02X" :"%03x" ;;
}
>void foo(int m) {
const char *str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
}
>因为你现在要分配给一个const char *,而不是一个非const char *,
问题消失了。
>Since you''re now assigning to a const char*, not to a non-const char*,
the problem disappears.
这样可行,但奇怪的是以下内容:
const char * str;
str = m< 4? "%01X" :m< 9? "%02X" :%03x;
if(m 12)str ="%04x";
似乎是const对于char *类型并不意味着和其他类型的
一样多。
Steve
That works, but oddly so does the following:
const char *str;
str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
if (m 12) str = "%04x";
It seems the "const" does not mean as much for type char * as
it does for other types.
Steve
>
致以最诚挚的问候,
Tom
>
Best regards,
Tom
Steve Pope写道:
Steve Pope wrote:
似乎是const对于类型char *而言,并不意味着
对于其他类型。
It seems the "const" does not mean as much for type char * as
it does for other types.
这是一个与char类型无关的问题,而是与字符串文字相关的问题:
允许以下转换:
string literal -const char *
string literal -char * //已弃用,但有效
但当然,它's无效
const char * -char *
所以,你可以直接将字符串文字转换成char *,而不是通过
const char *(你尝试使用条件运算符''''')。
再见,
Zeppe
It''s an issue related not to the char type, but to the string literals:
the following conversion are allowed:
string literal -const char*
string literal -char* // deprecated, but valid
but, of course, it''s not valid
const char* -char*
so, you can convert string literal to char* only directly, not through
const char* (as you tried with the conditional operator ''?'').
Bye,
Zeppe
这篇关于将字符串常量赋给char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!