如何访问嵌套在另一个结构内(也通过指针访问)的结构的成员(通过指针访问)?
我在嵌套结构中访问数据时遇到了困难。我试图实现两个MCU之间的SPI通信。 这个想法是为嵌套在处理程序中并在传输前序列化的其他功能提供易于操纵结构。 但是,在序列化过程中访问结构不起作用。
我尝试在下面创建一个简化的数据架构模型。
typedef struct a_t{
uint16_t member1;
uint8_t member2;
} a_t;
typedef struct b_t
{
volatile a_t* aPtr;
volatile uint8_t arrayC[3];
} b_t;
a_t a;
a.member1 = 0xABCD;
a.member2 = 0xEF;
b_t b;
b_t* bPtr = &b;
bPtr->aPtr = &a;
据我了解,结构 b 现在包含一个指向struct a 的指针,而 bptr 指向结构 b 。 因此,如果我退出,
bPtr->aPtr
我应该得到一个指向struct a 的指针。如果我进一步退出,我应该能够访问 a 的成员,这
bPtr->arrayC[0] = (((bPtr->aPtr->member1) >> 8) & 0xFF);
bPtr->arrayC[1] = ((bPtr->aPtr->member1) & 0xFF);
bPtr>arrayC[2] = bPtr->aPtr->member2;
应该导致包含以下数据的 arrayc :
bPtr->arrayC[0] == 0xAB;
bPtr->arrayC[1] == 0xCD;
bPtr->arrayC[2] == 0xEF;
可悲的是,我找不到是这样的我的代码。 下
bPtr->arrayC[0] == 0x00;
bPtr->arrayC[1] == 0x00;
bPtr->arrayC[2] == 0x00;
的
bPtr->arrayC[0] = 0xAB;
情况
(((bPtr->aPtr->member1) >> 8) & 0xFF);
((bPtr->aPtr->member1) & 0xFF);
bPtr->aPtr->member2;
在我 这是一个愚蠢的问题,我可以在其他帖子中找到一些双重指针参考。但是我找不到解释为什么我的解决方案在其他地方无法使用,因此,如果一个经验丰富的程序员能够向我解释我的错误,我将不胜感激。
编辑:我在模型中纠正了一个错误,在该模型中,我错误地增加了成员 bptr-> aptr 作为 b-> a
edit2:与The一起测试时的解释确实有效以下编译器
Godbolt.org/z/wdcahndxh
但是,相同的代码在我的MCU中产生0x00(MCU:MKE02Z处理器:Cortex M0+编译器:ARM6 C99)
I am having trouble accessing data in a nested Struct. I am trying to realize a SPI-Communication between two MCUs.
The Idea was to provide an easy to manipulate Struct to other functions which is nested in the handler and serialized before transmission.
Accessing the struct during serialization does not work however.
I tried to create a simplified model of my datastructure below.
typedef struct a_t{
uint16_t member1;
uint8_t member2;
} a_t;
typedef struct b_t
{
volatile a_t* aPtr;
volatile uint8_t arrayC[3];
} b_t;
a_t a;
a.member1 = 0xABCD;
a.member2 = 0xEF;
b_t b;
b_t* bPtr = &b;
bPtr->aPtr = &a;
To my Understanding, the struct b now contains a Pointer to struct a while bPtr points to struct b.
So if i dereference
bPtr->aPtr
i should get a Pointer to Struct a. If i dereference further, I should be able to access the members of a like this
bPtr->arrayC[0] = (((bPtr->aPtr->member1) >> 8) & 0xFF);
bPtr->arrayC[1] = ((bPtr->aPtr->member1) & 0xFF);
bPtr>arrayC[2] = bPtr->aPtr->member2;
Which should lead to arrayC containing the following data:
bPtr->arrayC[0] == 0xAB;
bPtr->arrayC[1] == 0xCD;
bPtr->arrayC[2] == 0xEF;
Sadly i cannot find this is the case with my code. In my Case the Values of arrayC are as followed if i try to assign them like shown above
bPtr->arrayC[0] == 0x00;
bPtr->arrayC[1] == 0x00;
bPtr->arrayC[2] == 0x00;
If I assign Values as followed
bPtr->arrayC[0] = 0xAB;
The Value changes as intended, so the error must be in the expression
(((bPtr->aPtr->member1) >> 8) & 0xFF);
((bPtr->aPtr->member1) & 0xFF);
bPtr->aPtr->member2;
I am Sorry if this is a silly question, I could find some double pointer references in other posts. But I did not find an explanation why my solution does not work elsewhere so i am grateful if a more experienced programmer is able to explain my mistake to me.
Edit: I corrected a mistake in the Model in which i wrongly addresed member bPtr->aPtr as b->a
Edit2: The dereference does work when tested with the following compiler
godbolt.org/z/Wdcahndxh
However the same code produces 0x00 within my MCU (MCU: MKE02Z Processor: Cortex M0+ Compiler: ARM6 C99)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很抱歉。我发现我的错误。我为初始化功能切换了参数的顺序,并错误地将指向RxBuffer的指针放入了Spi Handler的TXBufferPtr中。 rxbuffer是空的,因此仅产生0。
我感谢所有花时间帮助我的人。
我是相对较新的,我应该删除这个问题吗? Stackoverflow警告我,我可能会被阻止询问我是否这样做。
I am so Sorry. I found my mistake. I switched the order of the arguments for my initialization function and mistakenly put the pointer to the rxBuffer into the txbufferptr of the SPI-handler. The rxBuffer was empty, therefore yielding only 0.
I am Thankful for everyone who took the time to help me.
I am relatively new, should I delete this question? Stackoverflow warns me that i could be blocked from asking if I do this.
先生,
对不起的话,那是错误的,您必须从那里进行重构,您需要使用结构成员来存储地址,而不是结构名称:
Sir,
Sorry, that is wrong, you must refactor from there, you need to use a structure member to store the address, not the struct name: