【问题标题】:Don't understand compiler warning - "passing unsigned into to type unsigned int*"不理解编译器警告 - “将 unsigned into 传递给类型 unsigned int*”
【发布时间】:2021-09-29 16:28:10
【问题描述】:

我创建了一个结构类型

typedef struct component {
    char IPaddress[25];
    int serverPortNumber;
    unsigned int handle;
};

我创建了该结构的三个实例。我正在使用内置的 CVI 功能连接到 TCP 服务器。它具有我提供的以下形式和参数。

ConnectToTCPServerEx(
    pComponent->handle, pComponent->serverPortNumber, 
    pComponent->IPaddress, ClientTCPCB, NULL, TCP_TIMEOUT, TCP_ANY_LOCAL_PORT)

在此函数调用之前的代码行中,我使用以下表达式来获取我已经创建的已声明组件结构的地址。

struct component *pComponent = &SomeComponentStruct;

我的编译器返回以下内容

警告:不兼容的整数到指针转换传递 'unsigned int'到'unsigned int *'类型的参数;用 & 取地址

为什么我会收到此警告? CVI 函数期望 'handle' 参数是通过引用传递的无符号整数(以便可以写入)。结构 .handle 的成员确实是无符号整数。但在这种情况下,我将 指针传递给结构,然后使用 -> 运算符访问“句柄”。添加“&”是没有意义的,因为那样我就会将一个指针传递给一个指针。

可能是因为下面一行

struct component *pComponent = &SomeComponentStruct;

在我的代码中实际上是两行,我声明了一个指向类型结构组件的指针,然后使用一个函数为其分配一个实际贴花结构组件的地址(取决于 int 参数)。

struct component *pComponent = NULL;

pComponent = setComponent(component) //this function returns the address of a declared struct component

编译器没有捕捉到这个吗?

【问题讨论】:

  • @NateEldredge 你成功了。谢谢。

标签: c cvi


【解决方案1】:

pComponent->handle 是成员对象本身,而不是指向它的指针。 pComponent 是一个指针,是的,但是 -> 取消了它的引用。您确实需要执行&pComponent->handle,然后您将拥有一个指向对象的指针(而不是指向指针的指针)。完全正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 2016-04-14
    相关资源
    最近更新 更多