【问题标题】:C++ Function pointer incorrect typesC++ 函数指针类型不正确
【发布时间】:2015-10-23 08:51:25
【问题描述】:

我需要使用以下函数,但我遇到了 args 问题:

在这种情况下,没有设置 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 system, LPSTR IPAddress, PULONG length);

参数

cwbCO_SysHandle 系统 - 输入

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

LPSTR IPAddress - 输出

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

【问题讨论】:

  • 去掉&符号,很可能LPSTR已经是一个typedef,它是一个指针。

标签: c++ pointers types arguments lpstr


【解决方案1】:

我找到了文档,cwbCO_GetIPAddress

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

LPSTR IPAddress - 输出 指向一个 缓冲区 的指针,该缓冲区将以点分十进制表示法包含以 NULL 结尾的 IP 地址(格式为“nnn.nnn.nnn.nnn”,其中每个“nnn”的范围为 0到 255)。

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

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

另外,请确保使用 cwbCO_CreateSystemcwbCO_CreateSystemLike 初始化您的 system

【讨论】:

  • 我在 char 缓冲区中得到奇怪的字符。如何使char 缓冲区清晰易读?我的意思是,将其转换为字符串。我试过 string str = string(ipAddress);但是字符串是空的。我也试过stringstream,但它一直显示奇怪的字符。
  • @ProtectedVoid 你初始化你的system了吗?你检查cwbCO_GetIPAddress的返回值了吗?
  • 好的,cwbCO_SysHandle 错误。 cwbCO_CreateSystem 函数设置了一个 cwbCO_SysHandle 指针,我正在使用指针处理程序。我需要使用用于创建系统的处理程序。谢谢。
猜你喜欢
  • 1970-01-01
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-05
  • 1970-01-01
  • 2018-09-12
相关资源
最近更新 更多