简体   繁体   English

为什么它不能将 char* 转换为 char

[英]why it cannot convert char* to char

In c++ we can write在 C++ 中,我们可以写
1 char *s="hello" 1 个char *s="hello"

but the below lines of program produces an error ( cannot convert char* to char)但以下程序行产生错误(无法将 char* 转换为 char)
2 char *s; *s="hello"; 2个char *s; *s="hello"; char *s; *s="hello";

I am confused here, what is difference between 1 and 2 why this error is coming?我在这里很困惑,为什么会出现此错误,1 和 2 之间有什么区别?

In C++, a string literal is a constant array of characters, not just an array of characters like in C. Anyways, to assign to such a variable (Which is best avoided), you do not have to dereference the pointer.在 C++ 中,字符串文字是字符的常量数组,而不仅仅是像 C 中的字符数组。无论如何,要分配给这样的变量(最好避免这种情况),您不必取消引用指针。 Dereferencing it accesses the first element, which is just a char.取消引用它会访问第一个元素,它只是一个字符。 A char cannot hold an array of characters inside it, causing an error. char 不能在其中包含字符数组,从而导致错误。 This is more the reason why you should be using std::string .这更多是您应该使用std::string

Some compilers such as GCC provide extensions to make such code possible since it is not standards compliant code, and it would look like:一些编译器(例如 GCC)提供了扩展以使此类代码成为可能,因为它不是符合标准的代码,它看起来像:

char* s = "hello";
s = "new string";

This generates the following warning in GCC (But still gets the expected result):这会在GCC 中生成以下警告(但仍然得到预期的结果):

warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]警告:ISO C++ 禁止将字符串常量转换为 'char*' [-Wwrite-strings]

Clang also has the same behavior with the same output (Also generating a warning) Clang也具有相同输出的相同行为(同时生成警告)

A string is an array of characters.字符串是一个字符数组。 The start of a string therefore is const char * .因此,字符串的开头const char * Therefore to reference a string, you can use const char * s = "hello";因此,要引用字符串,可以使用const char * s = "hello";

However if you dereference a const char* , you get a const char .但是,如果您取消引用const char* ,则会得到一个const char This isn't a string ie *s gives you 'h'.这不是一个字符串,即*s给你 'h'。

In your code *s="hello";在你的代码中*s="hello"; , you are saying "assign at the dereferened s the value "hello"". ,你说的是“在被取消引用的地方分配值”你好“”。 Dereferencing s is a character only, to which you are trying to assign a string.取消引用 s 只是一个字符,您正在尝试为其分配一个字符串。

The problem is the second asterisk in your second example.问题是第二个示例中的第二个星号。

The first code is this第一个代码是这样的

char *s="hello";

The equivalent code is this等效代码是这样的

char *s; 
s="hello";

No * before s in the second line.第二行中的s前没有*

Now as everyone is pointing out neither of these are legal C++.现在每个人都指出这些都不是合法的 C++。 The correct code is正确的代码是

const char *s="hello";

or或者

const char *s; 
s="hello";

Because string literals are constant, and so you need a pointer to const char .因为字符串文字是常量,所以你需要一个指向const char的指针。

I am confused here, what is difference between 1 and 2 why this error is coming?我在这里很困惑,为什么会出现此错误,1 和 2 之间有什么区别?

As many others * in C++ means different things in different context:与 C++ 中的许多其他*在不同的上下文中意味着不同的东西一样:

char *s; // * here means that s type is a pointer to char, not just char
*s;      // in this context * means dereference s, result of exression is char
int a = 5 * 2; // in this context * means multiply

so case 1 and 2 may look similar to you but they mean very different things hence the error.因此案例 1 和案例 2 可能看起来与您相似,但它们的含义非常不同,因此会出现错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM