陷入C指针结构

时间:2012-02-20 18:51:36

标签: c pointers embedded structure zigbee

我正在开发Embedded C。我被指针结构困住了....

结构如下:

/*structure 1*/
ZPS_tsAplApsmeBindingTableType *psAplApsmeAibBindingTable;

/*structure 2*/
typedef struct
{
    ZPS_tsAplApsmeBindingTableCache* psAplApsmeBindingTableCache;
    ZPS_tsAplApsmeBindingTable* psAplApsmeBindingTable;
}ZPS_tsAplApsmeBindingTableType;

/*structure3*/
typedef struct
{
   uint64  u64SourceAddress;
   ZPS_tsAplApsmeBindingTableEntry* pvAplApsmeBindingTableEntryForSpSrcAddr;
   uint32 u32SizeOfBindingTable;
}ZPS_tsAplApsmeBindingTable;

/*structure 4*/
typedef struct
{
   ZPS_tuAddress  uDstAddress;
   uint16  u16ClusterId;
   uint8   u8DstAddrMode;
   uint8   u8SourceEndpoint;
   uint8   u8DestinationEndPoint;
} ZPS_tsAplApsmeBindingTableEntry;

我已声明ZPS_tsAplApsmeBindingTableType *p;但我想访问ZPS_tsAplApsmeBindingTableEntry结构值...我怎么能这样做?

任何人都可以告诉我

之间的区别
ZPS_tsAplApsmeBindingTable* psAplApsmeBindingTable

ZPS_tsAplApsmeBindingTable *psAplApsmeBindingTable;

谢谢....

4 个答案:

答案 0 :(得分:4)

  1. p->psAplApsmeBindingTable->pvAplApsmeBindingTableEntryForSpSrcAddr->someField
  2. 没有区别。
  3. PS。那段代码真的非常难看。

答案 1 :(得分:2)

ZPS_tsAplApsmeBindingTable* psAplApsmeBindingTable; 

ZPS_tsAplApsmeBindingTable *psAplApsmeBindingTable; 

ZPS_tsAplApsmeBindingTable * psAplApsmeBindingTable; 

相同*的位置不会改变任何内容。


要访问指针指向的结构的值(如指针p),可以使用箭头->

p->psAplApsmeBindingTable->pvAplApsmeBindingTableEntryForSpSrcAddr->u16ClusterId

答案 2 :(得分:1)

  

我已经声明了ZPS_tsAplApsmeBindingTableType * p;但我想访问ZPS_tsAplApsmeBindingTableEntry结构值......我怎么能这样做?

嗯,你不能。在您的代码中,ZPS_tsAplApsmeBindingTableType不包含ZPS_tsAplApsmeBindingTableEntryZPS_tsAplApsmeBindingTableEntry*类型的任何成员。

  

有谁能告诉我ZPS_tsAplApsmeBindingTable * psAplApsmeBindingTable和ZPS_tsAplApsmeBindingTable * psAplApsmeBindingTable之间的区别;

没有区别;它们是一样的...字面上相同的文本复制了两次。我真的不明白你的问题。如果你能详细说明一点,我可以进一步提供帮助。

答案 3 :(得分:0)

您将按以下方式访问ZPS_tsAplApsmeBindingTableEntry成员:

p->psAplApsmeBindingTable->pvAplApsmeBindingTableEntryForSpSrcAddr->uDstAddress
p->psAplApsmeBindingTable->pvAplApsmeBindingTableEntryForSpSrcAddr->u16ClusterId
p->psAplApsmeBindingTable->pvAplApsmeBindingTableEntryForSpSrcAddr->u8DstAddrMode

等。您可以使用->进行所有选择,因为ppsAplApsmeBindingTablepvAplApsmeBindingTableEntryForSpSrcAddr都是结构类型的指针。如果其中任何一个是指针类型,您将使用.运算符为该类型执行组件选择。例如:

struct a {int x; int y};
struct b {struct a *p; struct a v};
struct b foo, *bar = &foo;
...
foo.p->x = ...;  // foo is not a pointer type, p is a pointer type
bar->v.y = ...;  // bar is a pointer type, v is not a pointer type

表达式x->y(*x).y的简写; IOW,您取消引用x,然后选择y

声明之间没有区别

T *p;

T* p;

两者都被解释为T (*p); - *始终是声明符的一部分,而不是类型说明符。如果你写了

T* a, b;

只有a被声明为指向T的指针; b将被声明为普通T