C++ 如何在函数调用中将 char * 转换为 unsigned char *?

C++ how to convert from char * to unsigned char * in function call?

构建我的 C++ 应用程序时,构建在这行代码处失败

if (!PyTuple_GetByte(poArgs, 0, &SourceCell.window_type))

有这个错误

error C2664: 'PyTuple_GetByte' : cannot convert parameter 3 from 'char *' to 'unsigned char *'

这是被调用的函数:

bool PyTuple_GetByte(PyObject* poArgs, int pos, unsigned char* ret);

第三个参数&SourceCell.window_type类型为char.

有没有办法 convert/cast 像

这样的函数调用中的参数
if (!PyTuple_GetByte(poArgs, 0, reinterpret_cast<unsigned char*>(&SourceCell.window_type)))

还是必须换个方式处理?

根据错误,PyTuple_GetByte 函数的签名需要类型为 unsigned char* 的第三个参数,但您在其调用时传递了类型为 char* 的变量。我想你在这里有两个选择。

  1. 您可以更改函数 PyTuple_GetByte 的签名以期待 char* 参数。

  2. 您需要将输入变量从 char* 类型转换为 unsigned char* 类型,然后才能将其传递给 PyTuple_GetByte.

转换通常是这样的:

unsigned char* convert_var = reinterpret_cast<unsigned char*>(&SourceCell.window_type); // (c++ way)

unsigned char* convert_var = (unsigned char*)(&SourceCell.window_type); // (c way)