C ++编译器中的C代码

| 我有以下代码,它是tomcrypto手册中的代码,在MS VC ++ 2008 EE上不起作用。有什么帮助吗?我也可以要求将“ 0”替换为“ 1”对象吗?
int main(void)
{
hash_state md;
unsigned char *in = \"hello world\", out[16];
/* setup the hash */
md5_init(&md);
/* add the message */
md5_process(&md, in, strlen(in));
/* get the hash in out[0..15] */
md5_done(&md, out);
return 0;
}
错误:
\\main.cpp(7) : error C2440: \'initializing\' : cannot convert from \'const char [12]\' to \'unsigned char *\'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
.\\main.cpp(11) : error C2664: \'strlen\' : cannot convert parameter 1 from \'unsigned char *\' to \'const char *\'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
编辑:现在代码看起来像:
int main(void)
{
register_hash(&md5_desc);
hash_state md;
char* p = \"hello wordl\";
unsigned char *in = reinterpret_cast<unsigned char*>(p);
char* out[16];
/* setup the hash */
md5_init(&md);
/* add the message */
md5_process(&md, const_cast<char*>(in), strlen(in));
/* get the hash in out[0..15] */
md5_done(&md, out);
return 0;
}
错误:
\\main.cpp(21) : error C2440: \'const_cast\' : cannot convert from \'unsigned char *\' to \'char *\'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
.\\main.cpp(21) : error C2664: \'strlen\' : cannot convert parameter 1 from \'unsigned char *\' to \'const char *\'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
.\\main.cpp(23) : error C2664: \'md5_done\' : cannot convert parameter 2 from \'char *[16]\' to \'unsigned char *\'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    
已邀请:
unsigned char *in = \"hello world\"
这在C ++中是不正确的:
\"hello world\"
是字符串文字,类型为
const char[12]
。在C语言中,其类型为
char[12]
,但此处的
const
并不重要,因为在C ++中存在隐式(但已弃用)转换,该转换允许将字符串文字转换为
char*
。 问题在于
char
unsigned char
是不同的类型。 ѭ12是否为无符号无关紧要;三种
char
类型(
char
unsigned char
signed char
)都是截然不同的,在C ++中,如果没有强制转换,就无法在指向这三种类型的指针之间进行转换。 这在C语言中有效,因为在C语言中,您无需转换即可将任何指针到对象的类型转换为任何其他指针到对象的类型。在C ++中不是这种情况。 在C ++中,您需要使用:
// use the implicit conversion to \'char*\' to cast away constness:
char* p = \"hello world\";

// explicitly cast to \'unsigned char*\'
unsigned char* in = reinterpret_cast<unsigned char*>(p);
删除常量性通常不是一个好主意,因为字符串文字不可修改,但是有时在处理不符合const正确性的旧版库时有时是必要的。 从
char*
unsigned char*
的转换是安全的,因为在C ++中所有对象都可以视为
char
unsigned char
signed char
的数组。     
char
是与
signed char
unsigned char
不同的类型;字符串文字始终为
(const) char *
类型;因此您不能将它们指定为
(const) signed char *
(const) unsigned char *
。要解决此问题,请从第4行中删除
unsigned
。 如果您的
md5_process()
函数显式接受
unsigned char *
作为参数,那么您应该在此时进行强制转换:
md5_process(&md, reinterpret_cast<unsigned char*>(in), strlen(in));
[正如其他人所说的,您应该将
in
定义为
const char *in
,因为它指向字符串文字,但这不是问题。]     
让我们再试一次:
int main(void)
{
register_hash(&md5_desc);
hash_state md;
const char* p = \"hello wordl\";
const unsigned char* in = reinterpret_cast<const unsigned char*>(p);
unsigned char out[16];
/* setup the hash */
md5_init(&md);
/* add the message */
md5_process(&md, in, strlen(p));
/* get the hash in out[0..15] */
md5_done(&md, out);
return 0;
}
这样行吗?     
这是因为垃圾字符串在C ++中是const,而您使用非const指针对其进行了初始化:
const char* in = \"hello world\";
char * out[16];
但是,如果md5_process接受非常量
char*
,则可能会导致问题,在这种情况下,您将不得不强制转换为非常量:
md5_process(&md, const_cast<char*>(in), strlen(in));
    

要回复问题请先登录注册