C++ 中的 C 代码编译器

发布于 2024-11-02 13:44:02 字数 1972 浏览 4 评论 0原文

我有以下代码,它是来自 tomcrypto 手册的代码,它不适用于 MS VC++ 2008 EE。有什么帮助吗?另外我可以要求用 std::string 对象替换 char* 吗?

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

I have following code, it's code from tomcrypto's manual and it won't work on MS VC++ 2008 EE. Any help? Also can I ask replace char* by std::string object?

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;
}

Errors:

\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

EDIT: Now code looks like:

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;
}

Errors:

\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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

将军与妓 2024-11-09 13:44:02
unsigned char *in = "hello world"

这在 C++ 中是不正确的:"hello world" 是一个字符串文字,并且类型为 const char[12]。在 C 中,它的类型为 char[12],但这里的 const 并不重要,因为在 C++ 中,存在允许字符串文字的隐式(但已弃用)转换转换为 char*

问题是 charunsigned char 是不同的类型。 char 是否无符号并不重要;三种 char 类型(charunsigned charsigned 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);

删除常量通常是一个坏主意,因为字符串文字不可修改,但有时在处理不常量正确的遗留库时有必要。

char*unsigned char* 的转换是安全的,因为所有对象都可以被视为 charunsigned char< /code>,或 C++ 中的 signed char

unsigned char *in = "hello world"

This is incorrect in C++: "hello world" is a string literal and is of type const char[12]. In C it is of type char[12], but the const here doesn't matter because in C++ there is an implicit (but deprecated) conversion that allows a string literal to be converted to a char*.

The problem is that char and unsigned char are different types. It doesn't matter whether char is unsigned; the three char types (char, unsigned char, and signed char) are all distinct and in C++ you cannot convert between pointers to those three types without a cast.

This works in C because in C you can convert any pointer-to-object type to any other pointer-to-object type without a cast. That isn't the case in C++.

In C++ you would need to use:

// 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);

The removal of constness is usually a bad idea since string literals are not modifiable, but sometimes it is necessary when dealing with legacy libraries that are not const-correct.

The conversion from char* to unsigned char* is safe because all objects can be treated as an array of char, unsigned char, or signed char in C++.

雄赳赳气昂昂 2024-11-09 13:44:02

char 是与 signed charunsigned 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 因为它指向字符串文字,但是这不是这里的问题。]

char is a different type to signed char or unsigned char; string literals are always of type (const) char *; so you cannot assign them to a (const) signed char * or a (const) unsigned char *. To fix this, remove the unsigned from line 4.

If your md5_process() function explicitly takes an unsigned char * as an argument, then you should perform a cast at that point:

md5_process(&md, reinterpret_cast<unsigned char*>(in), strlen(in));

[As others have said, you should really define in as const char *in as it's pointing to a string literal, but that is not the issue here.]

转身以后 2024-11-09 13:44:02

让我们再试一次:

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;
}

这有效吗?

Let's try again:

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;
}

Does this work?

节枝 2024-11-09 13:44:02

这是因为在 C++ 中,文字字符串是 const,而您使用非常量指针初始化它:

const char* in = "hello world";
char * out[16];

但是,如果 md5_process 采用非常量 char*,则可能会导致问题,在这种情况下,您将必须转换为非常量:

md5_process(&md, const_cast<char*>(in), strlen(in));

This is because litteral strings are const in C++, while you initialize it with a non-const pointer:

const char* in = "hello world";
char * out[16];

However it might cause a problem if md5_process takes a non-const char*, in this case you'll have to cast to a non-const:

md5_process(&md, const_cast<char*>(in), strlen(in));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文