【问题标题】:getting stuck with C pointer Structure卡在 C 指针结构中
【发布时间】:2012-02-20 18:51:36
【问题描述】:

我正在研究嵌入式 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;

谢谢....

【问题讨论】:

  • 我坚持你的命名约定!哇。

标签: c pointers embedded structure zigbee


【解决方案1】:
  1. p->psAplApsmeBindingTable->pvAplApsmeBindingTableEntryForSpSrcAddr->someField
  2. 没有区别。

PS。这段代码真的很丑。

【讨论】:

  • 我认为 ZPS_tsAplApsmeBindingTable* psAplApsmeBindingTable 是一个结构数组...如果我使用 psAplApsmeBindingTable[0].someval 或 psAplApsmeBindingTable->someptr 它显示非法地址对齐
【解决方案2】:
ZPS_tsAplApsmeBindingTable* psAplApsmeBindingTable; 

ZPS_tsAplApsmeBindingTable *psAplApsmeBindingTable; 

ZPS_tsAplApsmeBindingTable * psAplApsmeBindingTable; 

是否相同* 的位置不会改变任何东西。


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

p->psAplApsmeBindingTable->pvAplApsmeBindingTableEntryForSpSrcAddr->u16ClusterId

【讨论】:

  • 但是先生,它显示错误.. ZPS_tsAplApsmeBindingTable* psAplApsmeBindingTable 它是由 psAplApsmeBindingTable 指向的结构数组.. 如果我再次使用 p->psAplApsmeBindingTable[0].u64SourceAddress 它的显示错误.. 请建议一个解决它的方法..
  • 你试过p->psAplApsmeBindingTable[0]->u64SourceAddress 吗?
【解决方案3】:

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

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

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

没有区别;它们是相同的东西...实际上是相同的文本复制了两次。我真的不明白你的问题。如果您能详细说明一下,我可能会提供进一步的帮助。

【讨论】:

  • 假设结构指针指向结构数组时 ZPS_tsAplApsmeBindingTable* psAplApsmeBindingTable 它是一个结构指针 .... 我想访问该位置中的那些值 ...
【解决方案4】:

您可以通过以下方式访问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

【讨论】:

    猜你喜欢
    • 2021-03-23
    • 2021-07-14
    • 2021-05-03
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    相关资源
    最近更新 更多