r/C_Programming • • 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 ≡ *(Absolute Register Address)

2. Simplified address-level expression

/* Dereference the absolute register address */

*(volatile uint32_t *)(Base_Address + Register_Offset)

3. Byte-level address calculation

/* Cast Peripheral to char * so pointer arithmetic operates in bytes rather than scaling by the peripheral pointer's type. */

*(volatile uint32_t *)((char *)Peripheral + Register_Offset)

4. The C standard's syntactic equivalence

/* LHS->RHS is defined as (*LHS).RHS */

(*Peripheral).Register

The idea is to move from the mental model, to the address calculation, and finally to the syntax defined by C.

0 Upvotes

15 comments sorted by

4

u/zhivago 23d ago

a->b is (*a).b is quite sufficient.

1

u/enoch_twumasi 23d ago

To some. In my investigations at that moment I came accross that. Did it make sense? Yeah as one who came to C with a great background in JavaScript and node.js I’m used to using dot notation in accessing properties/members in JS object. Of course I knew that’s all high level domain.

But for whatever reason, I didn’t wanna just settle on that in C. I suppose it all depends on what knowledge you had in the language before starting to really use the arrow operator.

I don’t think I had used that enough in C before I switched straight to the “Bare-metal Embedded C programming” book by Israel Gbati

1

u/zhivago 22d ago

Actually, I think your 1, 2, and 3 are wrong.

Remember that C does not have a flat memory model.

1

u/enoch_twumasi 22d ago

As of now this mental model actually still helps me.

But I’m curious; Care to throw more light on that?

1

u/zhivago 22d ago

int i;

Why is &i + 1 well defined while &i - 1 and &i + 2 are not?

2

u/ComradeGibbon 18d ago

Worth mentioning a comment from Walter Bright who developed the D language. He said the compiler actually doesn't need -> for pointers it could use . just fine.

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 23d ago

Actually that’s my experience and mental model I spent hours working, trying to investigate from different sources and asking lots of questions.

Just about 2 hours ago I did research how to get those to fit in a code block. That’s why you see it well organized and doubting if it’s by LLM.

But everything you see there didn’t come from an LLM or text book.

That’s how I study. I hate to study without developing a logical accurate model. And I always prefer a logical mental model that’s a bit accurate.

This helped me to finally move on with my studies. Because with this I have peace of mind that whenever I see that operator I had never used I will not forget the underlying mechanism. I was a bit more worried I will forget it’s a dereferencing expression.

Honestly this style of studying is what brought me to C when I was so set on using JavaScript for everything. Even writing node.js UDP sockets I always traced down through the layers from v8 C++ to libuv C , and kernel layers down to the NIC and back. Until finally I decided I need more power, as in, in controlling hardware.

By GOD’s grace I decided it’s time for another language: Just C.

1

u/enoch_twumasi 23d ago edited 23d ago

Well kudos to your supernatural power of knowing it all. This makes me laugh. Because I know ai was not smart enough to develop the mental model for me when I needed it.

In my bare-metal C programming using Israel Gbati’s book and previous C studies from Robert Seacord’s Effective C book (2nd edition ),

I didn’t know much about the arrow operator.
But I did know dereferencing and how to actually define registers without using struct. That’s how I hand-coded my baremetal-bringup codes found in my nucleo-h755zi-q-baremtal-bringup repo.

You could see in my implementations I never used anything like GPIOB_TypeDef or for port E, although I needed GPIOB MODER and ODR as well as those of GPIOE. Why because I only thought in: * , uint32_t, volatile etc but not in GPIOB -> ODR.

I did know Typedef because I used enums before switching from effective C to “Baremetal C Programming by Israel Gbati”.

This why when I got to Chapter 6 and encountered the use of arrow operators and typedef -ed structs for the peripherals, I reasoned my way backwards. Breaking it down from standalone Typedef like GPIO_Typedef into struct GPIO and continuously building mental models until I finally arrived on a much more granular construction where I could easily relate to based on my knowledge at the time.

Man that’s how you study if you ever dreamt of being the world’s best neurosurgeon like I did, until you get redirected into a child hood dream of creating stuff instead of just fixing stuff.

I Studied Forest Resources Technology, Biological sciences, Biomedical Sciences in three different great universities before enrolling in Computer Sciences at my 4th University.

It’s hard to break the habit you develop from wanting to understand deeply enough to heal people.

This is why I refused to just move on when I got to chapter 6 of Israel’s book.

People study as though they’re reading story books, but even in High School I often took a whole table at the library because I often had a bunch of books and encyclopedia infront of me when I was just studying something like electrostatics.

I could spend several hours on just a single page because I always kept digging until I fully understood the page. This made the pages following so easy to read and retain.

This is why it took me hours to develop a mental model that made sense to me. This is pretty much nothing huge in embedded development so I’m surprised you were so alarmed an educated man with wife and kids could come up with such a simple mental models.

I’ll tell you what, my professors were not that impressed when I did a tedious meta analysis on Biomarkers and Leukemic cells.

I know in my post it shows up so simple as though there went questions asked, or hours spent. But this is put together for your good and my own good so I could always come to it when I rusty from using the arrow operator over and over again and forget what it boils down to.

I asked questions trynna make sense of the use of the arrow operator with what I already knew.

And that’s how come I came up with this mental model. My wife could bare witness how I spent time on my phone asking questions and proving the accuracy of my mental model at each stage when I was trynna make logical sense of it with the knowledge I had at the moment, while we strolled about in our neighborhood with our daughter in a stroller

Did you feel the writing was too perfect?

Well by GOD’s grace I’m that educated. I didn’t write scientific papers all these years, only to make no sense while jotting down my mental model in a way it’s well understood and I could revisit when I needed to.

At least I know my physics teacher from high school would have agreed with me. You dare not go to the board to solve a physics problem without preparing to write as though a paper to be published. That's a bit of a stretch but yeah, You would write explicitly well without ambiguity in the comments above all that physics calculations even with white chalk on a green board and will explain to the entire class.
For what it's worth, he was nicknamed Sir Kirchhoff.

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/enoch_twumasi 22d ago edited 22d ago

I mean you’re write in many sense. But honestly I study better when I understand implementation to some extent.

In high school I was always behind due to how I study: “Trying to study in a way to retain fully”.

I was and still is always worried that the more knowledge I feed myself without understanding well enough, the more I may forget.

And I found more reason to believe that. I try so hard to hold onto what I studied (for example yesterday) such that I’m unable to move faster in today’s study.

But one thing that really has helped me by GOD’s grace, is knowing when I understand it well enough, I really hardly worry I may forget.

Because I’ve tried and I don’t forget. Which I thank my GOD for. Because I always prayed for the ability to retain the things I study to HIS Glory and also to overcome this “thing” that holds me back on my study, “the evident fear to forget”.

And usually for me, to understand it well enough requires understanding the implementation to some extent, or building a mental model that is a bit contextually logical and not easily defeated by other thoughts or concepts

1

u/sciencekm 23d ago

x.y is the same as (&x)->y

1

u/enoch_twumasi 23d ago

Yes, that’s correct in ordinary cases. (&x)->y accesses the same member as x.y. It’s essentially taking the address of x and accessing y through that pointer.
One interesting qualification: this isn’t universally interchangeable in C. For example, bit-fields can be accessed with ., but you cannot take their address. So the equivalence has some language-level edge cases.

2

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!