简体   繁体   English

“unsigned char *”类型的参数与“const char *”类型的参数不兼容

[英]Argument of type "unsigned char *" is incompatible with parameter of type "const char *"

I was using the Decoder for Microsoft Script Encoder.我使用的是 Microsoft Script Encoder 的解码器。 It works perfectly well when I run it in Codeblocks.当我在 Codeblocks 中运行它时,它运行良好。 But when I run it in Visual Studio, it shows me the following errors但是当我在 Visual Studio 中运行它时,它显示了以下错误
Snippet 1:片段 1:

char decodeMnemonic(unsigned char *mnemonic)
{
    int i = 0;
    while (entities[i].entity != NULL)
    {
        **if (strcmp(entities[i].entity, mnemonic) == 0)**
       **//Error 1: cannot convert argument 2 from 'unsigned char *'
       // to 'const char *'**   
        return entities[i].mappedchar;
        i++;
    }
    printf("Warning: did not recognize HTML entity '%s'\n", mnemonic);
    return '?';
}

I had to integrate the Decoder in a program, so instead of passing the filenames as command line arguments, I have given their filepaths myself in the code.我必须将解码器集成到程序中,因此我没有将文件名作为命令行参数传递,而是在代码中自己提供了它们的文件路径。

Snippet 2:片段 2:

    int main()
    {
        unsigned char *inname = "C:\\Users\\Karthi\\Desktop\\Project Winter 2018-19\\poweliks_sample\\poweliks_encoded_js.bin";
        unsigned char *outname = "C:\\Users\\Karthi\\Desktop\\Project Winter 2018-19\\poweliks_sample\\decoded1.txt";
        unsigned int cp = 0;
   //**Error 2: 'initializing': cannot convert from 'const char [87]' to 'unsigned char *'**    

You can use reinterpret_cast (for unsigned char* to const char* ).您可以使用reinterpret_cast (对于unsigned char*const char* )。 But if you go from const unsigned char* to a non const type, you have to use const_cast first, since reinterpret_cast cannot cast away a const .但是,如果您从const unsigned char*转到非const类型,则必须首先使用const_cast ,因为reinterpret_cast无法丢弃const

The paragraph below gives a brief overview, why your code did not work.下面的段落简要概述了为什么您的代码不起作用。

According to C99 Standard (similiar to other C standards), a string literal has static storage duration and its type is char[] The standard says:根据C99 标准(类似于其他 C 标准),字符串文字具有静态存储持续时间,其类型为char[]标准说:

If the program attempts to modify such an array, the behavior is undefined.如果程序尝试修改这样的数组,则行为未定义。

The reason why your program worked, when you used argv is, that argv is not considered as an array of string literals.当您使用argv时,您的程序工作的原因是,该argv不被视为字符串文字数组。 This means you can modify them.这意味着您可以修改它们。

Here are the solutions for your problems:以下是针对您的问题的解决方案:

Snippet 1: strcmp is a method to compare two C Strings.代码段 1: strcmp 是一种比较两个 C 字符串的方法。 It expects const char* types.它需要 const char* 类型。

int strcmp ( const char * str1, const char * str2 ); int strcmp ( const char * str1, const char * str2 ); You have two option to solve it:你有两个选择来解决它:

  1. Declare your method like this像这样声明你的方法

    char decodeMnemonic(const char *mnemonic)
  2. Use C++Strings and declare your method like this使用 C++Strings 并像这样声明你的方法

    char decodeMnemonic(std::string mnemonic)

If you use the second solutions you have to call the c_str()-Method to use it in strcmp如果使用第二种解决方案,则必须调用 c_str()-Method 才能在 strcmp 中使用它

if (strcmp(entities[i].entity, mnemonic.c_str()) == 0)

Or you use only C++-String: read here how to use it: http://www.cplusplus.com/reference/string/string/compare/或者你只使用 C++-String:在这里阅读如何使用它: http : //www.cplusplus.com/reference/string/string/compare/

Snippet 2: You can't use it like this because you have string literals which are arrays constant characters.代码段 2:您不能像这样使用它,因为您有字符串文字,它们是数组常量字符。 Please use C++-Strings.请使用 C++-Strings。 You work with C++, so use his features ( https://www.geeksforgeeks.org/stdstring-class-in-c/ )您使用 C++,因此请使用他的功能( https://www.geeksforgeeks.org/stdstring-class-in-c/

Anyway if you want to use it C-like: https://www.programiz.com/c-programming/c-strings无论如何,如果你想像 C 一样使用它: https : //www.programiz.com/c-programming/c-strings

char c[] = "abcd";
char c[50] = "abcd";

Or with const (C++)或使用 const (C++)

char *str1 = "string Literal";
const char *str2 = "string Literal";

暂无
暂无

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

相关问题 “ const char **”类型的参数与“ const char *”类型的参数不兼容 - Argument of type “const char **” is incompatible with parameter of type “const char *” “const char *”类型的默认参数与“char *”类型的参数不兼容 - Default argument of type "const char *" is incompatible with parameter of type "char *" “const char *”类型的参数与“char”类型的参数不兼容 - argument of type "const char *" is incompatible with parameter of type "char" C++ “char”类型的参数与“const char”类型的参数不兼容 - C++ Argument of type “char” is incompatible with parameter of type “const char” 类型“ const char *”的参数与类型“ char *”的参数不兼容。 但为什么? :( - Argument of type “const char*” is incompatible with parameter of type “char*”. But why? :( “const char *”类型的参数与“char *”类型的参数不兼容 - Argument of type “const char *” is incompatible with parameter of type “char *” 类型“ const char *”的参数与类型“ LPCWSTR”的参数不兼容 - argument of type “const char *” is incompatible with parameter of type “LPCWSTR” IntelliSense:“ const char *”类型的参数与“ LPCWSTR”类型的参数不兼容 - IntelliSense: argument of type “const char *” is incompatible with parameter of type “LPCWSTR” 类型“ const char *”的参数与类型“ Person”的参数不兼容 - Argument of type “const char*” is incompatible with parameter of type “Person” 2 IntelliSense:类型为“ const char *”的参数与类型为“ LPCWSTR”的参数不兼容 - 2 IntelliSense: argument of type “const char *” is incompatible with parameter of type “LPCWSTR”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM