2012-11-06 46 views
3
char* foo = "fpp"; //compile in vs 2010 with no problem 

虽然字符串文字是const char * type。
并且const类型不能被分配给非const类型。
所以我期望上面的代码失败或我错过了什么?“hello world”字符串文字可以分配给char *类型吗?

编辑:对不起,我完全忘记了编译器也会抛出警告。
我一直在看错误列表。
我忘了检查一下。

Edit2:我将我的项目警告级别设置为EnableAllWarnings(/ Wall),并且没有关于此的警告。
所以我的问题仍然有效。

+0

只是可以肯定(我看到C++代码)C或C++程序? – PiotrNycz

+0

我相信这将只能扔警告如果有的话,如果指定的char * =为const char * ... – anishsane

+0

可以尝试写最少的代码,编译成.O&采取objdump的的.o文件将的;只是为了看看这个fpp驻留在RO内存中还是RAM中。 – anishsane

回答

4

据我所知,在C中,在添加const之前,这是将字符串分配给指针的方式。

在C++中这是已弃用行为,但仍允许保持向后兼容性。所以不要使用它。

事实上,我相信C++ 11是完全无效的。

+0

好吧,我明白了.C++向后兼容C,并且const只在C++中添加,这是有道理的。 – kypronite

2

不完全。字符串文字可分配给char*类型。字符串文字不应该被修改。

这种奇怪的情况是为了在const存在之前向后兼容程序。

+0

不知道为什么这会得到一个反对票,因为我相信它是正确的? –

+0

@PaulR我也没有。 – john

+0

无论如何,我的投票形式都是一样的,甚至可以解决问题。 ;-) –

6

C++ 03弃用[Ref 1]使用不带const关键字的字符串文字。

[参考1]C++ 03标准:§4.2/ 2

A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer to char”; a wide string literal can be converted to an rvalue of type “pointer to wchar_t”. In either case, the result is a pointer to the first element of the array. This conversion is considered only when there is an explicit appropriate pointer target type, and not when there is a general need to convert from an lvalue to an rvalue. [Note: this conversion is deprecated. See Annex D. ] For the purpose of ranking in overload resolution (13.3.3.1.1), this conversion is considered an array-to-pointer conversion followed by a qualification conversion (4.4). [Example: "abc" is converted to “pointer to const char” as an array-to-pointer conversion, and then to “pointer to char” as a qualification conversion. ]

C++ 11简单地移除上述引文这意味着它是在C++ 11非法代码。

此前C++ 03,C++获得了它的字符串字面量,而不const关键字的声明,需要注意的是,同样是在C.

1

gcc -std=c++0x完全有效的警告一下:

a.cpp:5:14: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

所以,这仍然是允许的,但不推荐使用,因为字符串是常量。

1

没有这样的东西作为const类型。 Const关键字是一个所谓的类型限定符。它可以应用于任何指针类型,只是意味着指针指向的值不应该被修改。

你也可以运用const限定指针引用本身是这样的:

char* const p ="aaa"; 

这将保护指针变量的指向另一个字符串。

1

有一个特殊的隐式转换来支持这一点,因为它是在遗留代码常见的成语(常写const出现之前)。你的字符串文字的类型是char const[],你应该这样使用它。一个好的编译器会在上面提出警告,因为转换从引入那一刻起就被弃用了。

请注意,这不同于C,其中字符串文字的类型是char[](但试图修改它仍然是未定义的行为)。

1

你是在谈论C字符串,这实际上是焦炭的载体。在C++中,使用std::string类,并且创建一个常量字符串为const std::string

无论如何,编译器,以便存储字面字符串在源代码显示保留将来节目一块内存。这部分内存被认为是只读的,所以你应该用const char *指向它。它的大小正好是字符串的大小加上尾部零的一个额外位置,标记字符串的结尾。

编译器需要保持向后兼容性,所以他们还是接受文字由char *指出。但是,这是误导性的,因为您不应该修改可以存储在嵌入式系统ROM中的内存。

在我的系统,我使用铛:

$ clang --version 
Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0) 
Target: i386-pc-linux-gnu 
Thread model: posix 

在铛C编译器,这个代码编译没有错误:

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    char * str = "Hello, World!"; 

    printf("%s", str); 

    return EXIT_SUCCESS; 
} 

然而,同样的代码(稍作修改,例如作为标题的名称)编译为C++程序时会引发以下警告:

kk.cpp:6:15: warning: conversion from string literal to 'char *' is deprecated [-Wdeprecated-writable-strings] 
     char * str = "Hello, World!"; 
        ^
1 warning generated. 

希望能够帮助。

相关问题