简体   繁体   English

如何访问嵌套在另一个结构中的结构成员(通过指针访问)(也通过指针访问)?

[英]How to Access a member of a Struct ( accessed via pointer) nested within another Struct (also accessed via pointer)?

I am having trouble accessing data in a nested Struct.我在访问嵌套结构中的数据时遇到问题。 I am trying to realize a SPI-Communication between two MCUs.我正在尝试实现两个 MCU 之间的 SPI 通信。 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 .据我了解, struct b现在包含一个指向 struct abPtr指向 struct b的指针。 So if i dereference所以如果我取消引用

bPtr->aPtr 

i should get a Pointer to Struct a .我应该得到一个指向 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:这应该导致arrayC包含以下数据:

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在我的情况下,如果我尝试像上面所示那样分配它们, arrayC的值如下

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编辑:我纠正了模型中的一个错误,其中我错误地将成员bPtr->aPtr称为b->a

Edit2: The dereference does work when tested with the following compiler Edit2:使用以下编译器测试时,取消引用确实有效

godbolt.org/z/Wdcahndxh godbolt.org/z/Wdcahndxh

However the same code produces 0x00 within my MCU (MCU: MKE02Z Processor: Cortex M0+ Compiler: ARM6 C99)然而,相同的代码在我的 MCU 中产生 0x00(MCU:MKE02Z 处理器:Cortex M0+ 编译器:ARM6 C99)

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.我切换了初始化函数的参数顺序,并错误地将指向 rxBuffer 的指针放入了 SPI 处理程序的 txbufferptr。 The rxBuffer was empty, therefore yielding only 0. rxBuffer 是空的,因此只产生 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. Stackoverflow 警告我,我可能会被阻止询问我是否这样做。

Sir,先生,

To my Understanding, the struct b now contains a Pointer to struct a while bPtr points to struct b.据我了解,结构 b 现在包含一个指向结构 a 而 bPtr 指向结构 b 的指针。 So if i dereference所以如果我取消引用

Sorry, that is wrong, you must refactor from there, you need to use a structure member to store the address, not the struct name:对不起,这是错误的,你必须从那里重构,你需要使用结构成员来存储地址,而不是结构名称:

// b_t struct has a member named aPtr, by sure you must
// store the address of a here
b.aPtr = &a;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过指针访问结构成员 - How to access struct member via pointer to pointer 如何通过指针访问结构的第二个成员? - How to access second member of struct via pointer? 如何通过双指针访问结构中的结构成员? - How to access the members of a struct within a struct via a double pointer? SPARC程序集,如何访问结构的C指针 - SPARC assembly, How a C pointer to struct is accessed 初始化并访问C中另一个结构内的嵌套结构char指针 - Initialize and access a nested struct char pointer within another struct in C 指针算术通过先前的成员地址(在同一个结构中)导致指向另一个结构成员的指针 - Pointer arithmetic result in pointer to another struct member via previous member address (in the same struct) 通过指针访问结构的成员会导致错误 - accessing a member of a struct via pointer,results in error 给定一个指向结构内成员的指针,如何返回指向该结构的指针? - Given a pointer to a member within a struct, how to return a pointer to this struct? 可以使用指向结构成员的指针来访问同一结构的另一个成员吗? - Can a pointer to a member of a struct be used to access another member of the same struct? 指向使用索引(如数组)访问的结构的指针 - Pointer to a struct accessed using index like an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM