这是指向指针的指针吗?

时间:2014-10-08 09:57:41

标签: c++ c

考虑以下结构声明:

typedef struct
{
    UINT8  HdrLength:4;
    UINT8  Version:4;
    UINT8  TOS;
    UINT16 Length;
    UINT16 Id;
    UINT16 FragOff0;
    UINT8  TTL;
    UINT8  Protocol;
    UINT16 Checksum;
    UINT32 SrcAddr;
    UINT32 DstAddr;
} WINDIVERT_IPHDR, *PWINDIVERT_IPHDR;

" * PWINDIVERT_IPHDR"最后宣布,我以前从未见过这个。它似乎只是WINDIVERT_IPHDR结构的指针类型声明。我的假设是否正确?如果是的话,这个代码是否会:

extern BOOL WinDivertHelperParsePacket(PVOID pPacket, UINT packetLen,
    PWINDIVERT_IPHDR *ppIpHdr, PWINDIVERT_IPV6HDR *ppIpv6Hdr,
    PWINDIVERT_ICMPHDR *ppIcmpHdr, PWINDIVERT_ICMPV6HDR *ppIcmpv6Hdr,
    PWINDIVERT_TCPHDR *ppTcpHdr, PWINDIVERT_UDPHDR *ppUdpHdr, PVOID *ppData,
    UINT *pDataLen)

..特别是PWINDIVERT_IPHDR *ppIpHdr意味着方法参数" ppIpHdr"实际上有一个指向指针的指针?

2 个答案:

答案 0 :(得分:6)

是的,这是一个指针类型,如下面的代码段所示:

typedef struct A {
    int val;
} type, *type_ptr;

int main(int argc, const char *argv[]) {

  type obj;
  obj.val = 42;
  type_ptr ptr = &obj;
  std::cout << ptr->val; // 42

  return 0;
}

作为旁注:在C中,在声明对象或指向该对象的指针时,始终需要在struct关键字前加上前缀。这种typedef语法通常用于避免不必要的冗长。

答案 1 :(得分:4)

此typedef声明

typedef struct
{
    UINT8  HdrLength:4;
    UINT8  Version:4;
    UINT8  TOS;
    UINT16 Length;
    UINT16 Id;
    UINT16 FragOff0;
    UINT8  TTL;
    UINT8  Protocol;
    UINT16 Checksum;
    UINT32 SrcAddr;
    UINT32 DstAddr;
} WINDIVERT_IPHDR, *PWINDIVERT_IPHDR;

可以分为两个typedef声明

typedef struct
{
    UINT8  HdrLength:4;
    UINT8  Version:4;
    UINT8  TOS;
    UINT16 Length;
    UINT16 Id;
    UINT16 FragOff0;
    UINT8  TTL;
    UINT8  Protocol;
    UINT16 Checksum;
    UINT32 SrcAddr;
    UINT32 DstAddr;
} WINDIVERT_IPHDR;

typedef WINDIVERT_IPHDR *PWINDIVERT_IPHDR;

因此参数声明

PWINDIVERT_UDPHDR *ppUdpHdr

相当于

WINDIVERT_IPHDR * *ppUdpHdr