字符串C ++中不存在的常量 [英] String Constant absent in C++
问题描述
字符串常量在C ++中是可修改的,但在C中不可修改。
即
在C ++中,以下示例输出将是Mplusplus
char * str1 =" Cplusplus";
* str1 =''M'';
在C中,上面的例子会产生错误,因为我们试图修改一个常量。
因此,str1不是C ++中的字符串常量。
这是否意味着C ++中不存在字符串常量概念?
优先购买者,
Karthik Balaguru
Hi,
String constant being modifiable in C++ but, not modifiable in C.
that is,
In C++, the following example output will be "Mplusplus"
char *str1 = "Cplusplus";
*str1 = ''M'';
In C, the above example will produce an error as we are trying to
modify a constant.
So, str1 is not a string constant in C++.
Does it mean that string constant concept is absent in C++ ?
Thx in advans,
Karthik Balaguru
推荐答案
karthikbalaguru写道:
karthikbalaguru wrote:
字符串常量在C ++中是可修改的,但在C中不可修改。
即
在C ++中,以下示例输出将为Mplusplus
char * str1 =" Cplusplus";
* str1 =''M'';
在C中,上面的示例将为p因为我们试图修改一个常量而导致错误。
因此,str1不是C ++中的字符串常量。
这是否意味着C ++中不存在字符串常量概念?
Hi,
String constant being modifiable in C++ but, not modifiable in C.
that is,
In C++, the following example output will be "Mplusplus"
char *str1 = "Cplusplus";
*str1 = ''M'';
In C, the above example will produce an error as we are trying to
modify a constant.
So, str1 is not a string constant in C++.
Does it mean that string constant concept is absent in C++ ?
这是C ++委员会做出的非常糟糕的决定之一。
字符串文字的类型为const char []但是,由于我无法理解的原因,该标准允许将sting文字(并且只有
字符串文字)分配给char * ptr。通过隐式const
施法。更有可能的是,尝试修改字符串文字将
导致您的程序被segv。去图。
This is one of those really bad decisions by the C++ committee.
String literals are of type const char [], however, for reasons of
wisdom beyond my grasp, the standard allows sting literals (and only
string literals) to be assigned to a "char * ptr" by implicitly const
casting. More than likely, trying to modify the string literal will
cause your program to segv. Go figure.
karthikbalaguru写道:
karthikbalaguru wrote:
字符串常量为可以在C ++中修改,但在C中不可修改。
即
在C ++中,以下示例输出将为Mplusplus
char * str1 =" Cplusplus";
* str1 =''M'';
在C中,上面的例子会产生错误,因为我们正在尝试
修改一个常量。
Hi,
String constant being modifiable in C++ but, not modifiable in C.
that is,
In C++, the following example output will be "Mplusplus"
char *str1 = "Cplusplus";
*str1 = ''M'';
In C, the above example will produce an error as we are trying to
modify a constant.
不,它不会,因为str1不是常量。
一些C ++编译器会警告字符串文字被分配给
a char *
No it won''t, because str1 isn''t a const.
Some C++ compilers will warn that a string literal is being assigned to
a char*
所以,str1不是字符串C ++中的常量。
So, str1 is not a string constant in C++.
正确,也不是C中的一个。它可能指向一个字符串文字,但它没有声明为
。
Correct, nor is it one in C. It may point to a string literal, but it
isn''t declared const.
这是否意味着C ++中不存在字符串常量概念?
Does it mean that string constant concept is absent in C++ ?
当然不是。
cat xc
int main(){
const char * str1 =" Cplusplus" ;;
* str1 =''M'';
}
gcc -Wall -ansi -pedantic xc
/ tmp / xc:3:错误:分配只读位置
g ++ -Wall -ansi -pedantic xc
/ tmp / xc:3 :错误:分配只读位置
-
Ian Collins。
Of course not.
cat x.c
int main() {
const char *str1 = "Cplusplus";
*str1 = ''M'';
}
gcc -Wall -ansi -pedantic x.c
/tmp/x.c:3: error: assignment of read-only location
g++ -Wall -ansi -pedantic x.c
/tmp/x.c:3: error: assignment of read-only location
--
Ian Collins.
On 2007-09-02 06:43:07 -0400,Gianni Mariani< gi ******* @ mariani.wssaid:
On 2007-09-02 06:43:07 -0400, Gianni Mariani <gi*******@mariani.wssaid:
karthikbalaguru写道:
karthikbalaguru wrote:
>
字符串常量可在C中修改++但是,不能在C中修改。
即,在C ++中,以下示例输出将是Mplusplus
char * str1 =" Cplusplus";
* str1 =''M'';
在C中,上面的例子会产生错误,因为我们试图修改常量。
所以,str1不是C ++中的字符串常量。
这是否意味着C ++中不存在字符串常量概念?
>Hi,
String constant being modifiable in C++ but, not modifiable in C.
that is,
In C++, the following example output will be "Mplusplus"
char *str1 = "Cplusplus";
*str1 = ''M'';
In C, the above example will produce an error as we are trying to
modify a constant.
So, str1 is not a string constant in C++.
Does it mean that string constant concept is absent in C++ ?
这是C ++委员会做出的非常糟糕的决定之一。
字符串文字的类型为const char []但是,由于我无法理解的原因,该标准允许将sting文字(并且只有
字符串文字)分配给char * ptr。通过隐式const
施法。更有可能的是,尝试修改字符串文字将
导致您的程序被segv。去搞清楚。
This is one of those really bad decisions by the C++ committee.
String literals are of type const char [], however, for reasons of
wisdom beyond my grasp, the standard allows sting literals (and only
string literals) to be assigned to a "char * ptr" by implicitly const
casting. More than likely, trying to modify the string literal will
cause your program to segv. Go figure.
原因是历史上,C风格的字符串并不总是常量,并且有代码依赖于此。 C ++中的规则是
,字符串文字的类型是array-of-const-char。文字
可以转换为指向字符的指针,但是你写的是
你的危险。这保留了现状。转换是不推荐使用
,因此便携式代码不应该依赖它。
-
Pete
Roundhouse Consulting,Ltd。( www.versatilecoding.com )The的作者
标准C ++库扩展:教程和参考
( www.petebecker.com/tr1book )
The reason is that historically, C-style strings were not always
constant, and there was code that relied on this. The rule in C++ is
that the type of a string literal is array-of-const-char. The literal
can be converted into a pointer-to-char, but that you write to it at
your peril. That preserves the status quo. The conversion is
deprecated, so portable code should not rely on it.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
这篇关于字符串C ++中不存在的常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!