C++ 函数指针类型错误

标签 c++ pointers types arguments lpstr

我需要使用以下函数,但我在参数方面遇到了麻烦:

在这种情况下,未设置 IP 地址。

cwbCO_SysHandle system;
LPSTR ipAddress = "";
ULONG ipLength = 32;
cwbCO_GetIPAddress(system, ipAddress, &ipLength);

我知道我需要将指针传递给 LPSTR 作为参数,但设置以下代码也不起作用:

cwbCO_SysHandle system;
LPSTR ipAddress = "";
ULONG ipLength = 32;
cwbCO_GetIPAddress(system, &ipAddress, &ipLength); //Incompatible types LPSTR* and LPSTR

正确的做法是什么?

语法

UINT CWB_ENTRY cwbCO_GetIPAddress(cwbCO_SysHandle 系统, LPSTR IPAddress, PULONG 长度);

参数

cwbCO_SysHandle系统-输入

Handle that previously was returned by cwbCO_CreateSystem or cwbCO_CreateSystemLike. It is the
IBM i identification.

LPSTR IP 地址 - 输出

Pointer to a buffer that will contain the NULL-terminated IP address in dotted-decimal notation (in
the form "nnn.nnn.nnn.nnn" where each "nnn" is in the range of from 0 to 255).

PULONG 长度 - 输入/输出

Pointer to the length of the IPAddress buffer. If the buffer is too small to hold the output, including
room for the terminating NULL, the size of the buffer

最佳答案

我找到了文档,cwbCO_GetIPAddress

这里的相关部分是(强调):

LPSTR IPAddress - output Pointer to a buffer that will contain the NULL-terminated IP address in dotted-decimal notation (in the form "nnn.nnn.nnn.nnn" where each "nnn" is in the range of from 0 to 255).

所以你的代码应该看起来更像这样:

cwbCO_SysHandle system;
char ipAddress[32]; //A buffer, not a pointer!
ULONG ipLength = 32;
cwbCO_GetIPAddress(system, ipAddress, &ipLength);

此外,请确保使用 cwbCO_CreateSystemcwbCO_CreateSystemLike 初始化您的系统

关于C++ 函数指针类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31750172/

相关文章:

c++ - 我应该删除作为参数传递给函数的指针吗?

ios - iPhone NSXMLParser 解析字符串并存储在 NSNumber 变量和不同的数据类型中

oop - 在 Julia 中重新定义类型 : invalid redefinition of constant

c++ - 枚举对象名称

c++ - SDL 游戏在加载二级时卡住

c++ - 如何调用对象的析构函数,如果它的指针地址存储为无符号整数,这是唯一已知的东西?

c++ - 处理指针 c++ 的段错误(这次有更多代码)

在 C 中进行混合类型计算?

c++ - 在 Qt 中将 x 可执行文件作为按钮事件运行时出错

c++ - 如何 std::thread sleep