简体   繁体   English

无法将const char *转换为char *

[英]Cannot convert const char * to char *

Visual Studio c++ 2005 Visual Studio c ++ 2005

I am getting an error on the last line of this code. 我在这段代码的最后一行收到错误。

int Utils::GetLengthDiff ( const char * input, int & num_subst ) 
{
    int num_wide = 0, diff = 0 ; 
    const char * start_ptr = input ; 

    num_subst = 0 ; 
    while ( ( start_ptr = strstr ( start_ptr, enc_start ) ) != NULL ) 
    {
        char * end_ptr = strstr ( start_ptr, enc_end ); // Error

So I changed the line to this and it worked ok 所以我改变了这一行,它运作正常

const char * end_ptr = strstr ( start_ptr, enc_end ); 

So why would I need to declare end_ptr as a const as well? 那么为什么我还需要将end_ptr声明为const呢?

Many thanks, 非常感谢,

C++ has two overloaded versions of this function. C ++有两个这个函数的重载版本。 http://www.cplusplus.com/reference/clibrary/cstring/strstr/ http://www.cplusplus.com/reference/clibrary/cstring/strstr/

const char * strstr ( const char * str1, const char * str2 );
      char * strstr (       char * str1, const char * str2 );

Since your start_ptr is const char * the C++ compiler resolves to call the version that takes a const char * as the first parameter, that version also returns a const char * , so you have to change your return value to match. 由于你的start_ptrconst char * ,C ++编译器解析为调用以const char *作为第一个参数的版本,该版本也返回一个const char * ,因此你必须更改你的返回值才能匹配。

So why would I need to declare end_ptr as a const as well? 那么为什么我还需要将end_ptr声明为const呢?

For the same reason that start_ptr needs to be const char* : strstr returns the type const char* (= char const* ) because it searches inside a constant string (the parameter you pass to strstr is also const char* ). 出于同样的原因, start_ptr需要是const char*strstr返回类型const char* (= char const* ),因为它在常量字符串内搜索(传递 strstr的参数也是const char* )。 In particular, it's not the pointer that is const , it's the memory it points to. 特别是,它不是指针是const ,而是它指向的内存。 Think of it as a pointer to an immutable (ie constant) string. 可以将其视为指向不可变(即常量)字符串的指针。 You can change what it points to but not the individual characters inside the string. 可以更改它指向的内容,但不能更改字符串中的单个字符。

This is different from an unchangeable pointer which points to a mutable string, ie a string where you can change individual characters. 这与指向可变字符串的不可更改指针不同,即可以更改单个字符的字符串。

Suppose that the return value from strstr were char* , with a const char* first parameter, as it is in C. Then you could write: 假设strstr的返回值是char* ,带有const char* first参数,就像在C中一样。然后你可以写:

const char *s = "hello, world";
strstr(s, "hello")[0] = 'j';

The code would compile and run (with undefined behavior), but it's the kind of error which const was specifically designed to avoid. 代码将编译并运行(具有未定义的行为),但这是const专门设计为避免的错误。 You've converted a const char* to char* without a cast. 你已经将const char*转换为char*而没有const char*转换。

C can't really do anything about it: if strstr returned const char* then you'd have to cast back to non-const explicitly in the case where the input is non-const and you want to modify the string. C无法真正做任何事情:如果strstr返回const char*那么你必须在输入非const并且你想要修改字符串的情况下显式地转换回非const。 Because C++ has function overloading it can (and does) plug the loophole and make both cases work correctly. 因为C ++有函数重载,它可以(并且确实)插入漏洞并使两个案例都能正常工作。 Hence in C++, the above code fails to compile, and so does your example code. 因此在C ++中,上面的代码无法编译,您的示例代码也是如此。

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

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