![](/img/trans.png)
[英]Argument of type “const char **” is incompatible with parameter of type “const char *”
[英]Argument of type "unsigned char *" is incompatible with parameter of type "const char *"
我使用的是 Microsoft Script Encoder 的解码器。 当我在 Codeblocks 中运行它时,它运行良好。 但是当我在 Visual Studio 中运行它时,它显示了以下错误
片段 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 '?';
}
我必须将解码器集成到程序中,因此我没有将文件名作为命令行参数传递,而是在代码中自己提供了它们的文件路径。
片段 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 *'**
您可以使用reinterpret_cast
(对于unsigned char*
到const char*
)。 但是,如果您从const unsigned char*
转到非const
类型,则必须首先使用const_cast
,因为reinterpret_cast
无法丢弃const
。
下面的段落简要概述了为什么您的代码不起作用。
根据C99 标准(类似于其他 C 标准),字符串文字具有静态存储持续时间,其类型为char[]
标准说:
如果程序尝试修改这样的数组,则行为未定义。
当您使用argv
时,您的程序工作的原因是,该argv
不被视为字符串文字数组。 这意味着您可以修改它们。
以下是针对您的问题的解决方案:
代码段 1: strcmp 是一种比较两个 C 字符串的方法。 它需要 const char* 类型。
int strcmp ( const char * str1, const char * str2 ); 你有两个选择来解决它:
像这样声明你的方法
char decodeMnemonic(const char *mnemonic)
使用 C++Strings 并像这样声明你的方法
char decodeMnemonic(std::string mnemonic)
如果使用第二种解决方案,则必须调用 c_str()-Method 才能在 strcmp 中使用它
if (strcmp(entities[i].entity, mnemonic.c_str()) == 0)
或者你只使用 C++-String:在这里阅读如何使用它: http : //www.cplusplus.com/reference/string/string/compare/
代码段 2:您不能像这样使用它,因为您有字符串文字,它们是数组常量字符。 请使用 C++-Strings。 您使用 C++,因此请使用他的功能( https://www.geeksforgeeks.org/stdstring-class-in-c/ )
无论如何,如果你想像 C 一样使用它: https : //www.programiz.com/c-programming/c-strings
char c[] = "abcd";
char c[50] = "abcd";
或使用 const (C++)
char *str1 = "string Literal";
const char *str2 = "string Literal";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.