r/C_Programming • u/enoch_twumasi • 23d ago
A mental model for understanding the -> operator (GPIOB->ODR)
While working with memory-mapped peripheral registers in C, I developed this mental model for understanding what the arrow operator represents.
1. Absolute register address
/* LHS->RHS ≡ *(Absolute Address of RHS)
Absolute Address = Peripheral Base Address + Register Offset */
Peripheral->Register ≡ *
2. Simplified address-level expression
/* Dereference the absolute register address */
*
3. Byte-level address calculation
/* Cast Peripheral to char * so pointer arithmetic operates in bytes rather than scaling by the peripheral pointer's type. */
*
4. The C standard's syntactic equivalence
/* LHS->RHS is defined as (*LHS).RHS */
.Register
The idea is to move from the mental model, to the address calculation, and finally to the syntax defined by C.
0
Upvotes
4
u/zhivago 23d ago
a->b is (*a).b is quite sufficient.
7
u/ironykarl 23d ago edited 23d ago
Did you develop that mental model?
Cuz that looks quite a bit like LLM output
EDIT: I want to clarify, here. This is not me being anti-LLM. If you can use a LLM to actually aide your learning, then go for it.
What you instead seem to have done is circumvented using your brain and had an LLM generate some output for you.
This is actually the exact opposite of developing a mental model. Clearly you didn't develop this, but I would also speculate that if quizzed on this subject, you would in fact not have a usable, reference-able mental model.
You have to actually turn on your brain at some point to have a mental model
1
u/enoch_twumasi 22d ago edited 22d ago
You said x->y is then a simple dereference of a pointer pointing to a structure?
I think that’s not entirely right. That’s why my mental model simplifies the whole things as x-y
>y, in my case as an Embeded engineer, GPIOE -> ODR is ultimate *(the absolute memory address of ODR Register)
Which for short without this CMSIS style would have written as
#define GPIOE_ODR *((volatile uint32_t *)(GPIOE_BASE + GPIOE_ODR_OFFSET))
Of which we all know the expression “GPIOE_BASE + GPIOE_ODR_OFFSET” evaluates to the absolute memory address of ODR.
Look, GPIOE -> ODR is pretty much doing same things as the below:
#define GPIOE_ODR *((volatile uint32_t *)(GPIOE_BASE + GPIOE_ODR_OFFSET))
That’s why I reasoned that, that the expression with the -> operator which in my generalized mental model I commented as LHS -> RHS is basically the dereferencing of the absolute address of of the RHS (Right handside).
therefore to me, x->y is not simply dereferencing just a pointer to a structure.
Because that will mean, GPIOE-> ODR is simply dereferencing GPIOE which was probably defined as a pointer to a struct (#define GPIOE ((GPIO_TypeDef *) GPIOE_BASE).
And that is definitely what an engineer will be trying to do.
We want to access the Register ultimately not just the peripheral base.
Remember the peripheral base is what was casted to the struct type. ODR or the register we’re trying to access (RHS of the ->) is of the type uint32_t which you may qualify with the volatile keyword
1
u/ventZl 23d ago
You are making it more complicated than it really is.
The data type which models the peripheral translates the register layout of peripheral into formal specification understood by the programming language.
What HALs usually do is they type cast a raw peripheral base address as a pointer to said type "tricking" the compiler to treat it as variable having type which matches the physical layout of peripheral registers.
x->y is then simple dereference of pointer pointing to structure type.
I'd suggest stopping caring for what -> really does for a while and focusing on how a structure looks like in memory and why it is used to represent peripheral memory layout. Then how pointers in C work and what they are for.
Meaning and use of -> operator should then be obvious.
My personal addendum would be: You should not care about it. That's one mistake embedded developers do. They care about actual implementation too much and they grow unable to work with abstractions. This leads to many embedded devs being unable to switch stacks because they don't know how the new stack works and they can't use abstractions to map properties of one stack to another. And quite often it also leads to ugly untestable code being written.
1
u/sciencekm 23d ago
x.yis the same as(&x)->y2
u/rubidus-api 22d ago
I think there are two ways of looking at C.
One sees C as a thin, almost one-to-one translator into machine code. From that view, you read a program by asking what machine code comes out: which numeric addresses move through which registers on a particular machine.
The other defines C as a set of promises about an abstract machine. As long as the program keeps those promises and produces the promised results, the compiler may choose any steps and any means it likes.
C is clearly closer to the second. That was almost unavoidable once standardization began: the committee had to write rules that fit the many implementations and machines that already existed, so to stay portable and find their common ground, the language had to be defined more abstractly.
Compilers push the same way. With heavy optimization, statements are reordered, merged or removed, so C code and machine code no longer map one to one, if they ever fully did.
People learn in different ways. Some like the big picture first; others, as you mentioned, learn best by seeing how things work underneath. Both are fine, and your address picture is genuinely useful one layer down, for example when you check code against the register table in a datasheet. But for C I think the abstract view should come first, because that's how the language is defined. C is often called the language closest to the hardware, but it's defined far more abstractly than that reputation suggests.
More concretely, here's why I think tying GPIOB->ODR, or any obj_ptr->member, to its implementation can mislead.
First, a pointer is not just an address. At run time it's usually an integer in a register, but not always even that: on CHERI, a pointer is a 128-bit capability on a 64-bit machine, carrying bounds and permissions along with the address. In the C standard, a pointer is an abstract reference to an object, not a hardware address. Virtual memory, segment:offset addressing and tagged pointers all rely on that, and there have long been machines whose null pointer isn't all-bits-zero. On your Cortex-M, a pointer really is a plain 32-bit address in practice, and casts like (GPIO_TypeDef *)GPIOB_BASE work because the compiler documents them; that's the implementation's promise, not the language's. I think that's also what zhivago's &i + 1 question is getting at: C defines pointer arithmetic only within one object and one past its end, not over a flat address space.
So I'd take obj_ptr->member at its abstract meaning, which is really zhivago's (*a).b: the member member of the object that obj_ptr points to. How that becomes a physical location and which instructions come out is up to the compiler, and it has a lot of freedom there. I think this also helps with the question you discussed with ventZl: *obj_ptr doesn't read the whole struct, it only names the object. The one actual access happens at the member, when you read it or assign to it, so "dereference a pointer to the struct" and "dereference the register's address" end up as the same access.
One more point: strict aliasing and provenance. In C, two pointers to the same address aren't necessarily "the same". Strict aliasing lets the compiler assume that pointers to unrelated types don't refer to the same object. Provenance means a pointer has to come from a real object through valid operations; ISO published a technical specification on it (TS 6010) in 2025. Compilers rely on both to reorder and optimize loads and stores. Your step 3, (char *)Peripheral + Register_Offset cast to another type, is where these rules start to matter: it works for volatile registers, but the same habit on ordinary structs can run into them. So it pays to think of pointers at this more abstract level.
So my view of your mental model is that it takes a long way around to reach something you can reach directly, and the detour makes misconceptions easier to pick up.
I don't mean to belittle your approach or say it's wrong. This is just the view of someone who learned C from a similar angle and hit a few of those traps, as many people have.
I may have missed your point somewhere; understanding someone else's thinking is always hard. I hope you find something useful here anyway. Good luck with your studies!