不再支持从字符串常量转换为“char *” [英] deprecated conversion from string constant to 'char*'

查看:463
本文介绍了不再支持从字符串常量转换为“char *”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Possible Duplicate:
C++ deprecated conversion from string constant to 'char*'

我想通过char *传递一个字符串到一个函数。

I want to pass a string via char* to a function.

 char *Type = new char[10];
 Type = "Access";  // ERROR

但是,我收到此错误:

 error: deprecated conversion from string constant to 'char*'

如何解决?

推荐答案

如果您真的要修改类型:

If you really want to modify Type:

char *Type = new char[10];
strcpy( Type, "Access" );

如果您不想修改访问权限:

If you don't want to modify access:

const char *Type = "Access";

请注意,然而,C和C ++中的char数组有很多问题。例如,你不知道对新的调用是否成功,或者它是否会抛出异常。此外,strcpy()可以超过10个字符的限制。

Please note, that, however, arrays of char in C and in C++ come with a lot of problems. For example, you don't really know if the call to new has been successful, or whether it is going to throw an exception. Also, strcpy() could surpass the limit of 10 chars.

因此,如果你想以后修改类型:

So you can consider, if you want to modify type later:

std::string Type = "Access";

如果您不想修改它:

const std::string Type = "Access";

...使用 std :: string 是它能够处理所有这些问题。

... the benefit of using std::string is that it is able to cope with all these issues.

这篇关于不再支持从字符串常量转换为“char *”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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