Every Variable Lives Somewhere
When I first started learning about pointers, I made the mistake of trying to understand the symbols before understanding what they represented. I would see an asterisk here, an ampersand there, and try to memorize what each one meant. Sometimes it worked. But realistically, I was completely lost and too afraid to admit it.
And I don't think I was alone.
I've met plenty of intelligent people who can write excellent software but have difficulty explaining certain fundamentals. There's nothing particularly unusual about that. We learn what we need to learn, build what we need to build, and move on.
But eventually, I started asking myself a question.
Do I actually understand what's happening, or have I simply learned how to make it work?
There's a difference. A very important difference.
And pointers turned out to be a wonderful place to explore it.
Every Variable Has an Address
Here's something I wish somebody had explained to me in college.
Every variable lives somewhere in memory.
It's a simple idea, but an important one. When we write a variable, we usually think about its name and its value. We rarely stop to think about where that value is actually stored.
Think of your computer's memory as an enormous street with billions of houses. Every house has a number, and every house can hold one byte of information.
That number is its address.
It's a remarkable system when you think about it. Every location has an address, and the computer uses those addresses to find the information it needs.
Now consider a simple declaration in C.
int x = 42;Nothing particularly exciting here. We're declaring an integer named x and giving it the value 42.
But something interesting happens underneath.
The program needs somewhere to store that integer. On many modern computers, an int occupies four bytes, so we can imagine four neighboring houses being used to store our value.
MEMORY
Address Contents
────────────────────────────
0x1000 ┌──────────────┐
│ │
│ x = 42 │
│ │
0x1003 └──────────────┘For simplicity, I've given our variable the address 0x1000. The actual address depends on the program and the environment in which it runs.
Now here's the important distinction.
x is the name we use in our program. The address identifies where the object is stored in your computer's memory.
Isn't that fascinating?
Somebody designed a programming language that allows us to describe, access, and manipulate memory in an actual physical computer.
Not some imaginary machine.
The computer sitting right in front of us.
And we can do it using a language that has been publicly available for decades.
Think about that for a moment.
We write a few lines of text, compile them, and the machine executes instructions that operate on memory. Electrical circuits, transistors, and physical hardware, all working together to carry out what we've written.
I think that's extraordinary.
Of course, modern operating systems introduce another layer through virtual memory. The addresses our programs normally see are virtual addresses, which the operating system and hardware translate into physical memory locations.
But the underlying principle remains remarkable.
We're writing instructions that ultimately control the behavior of a physical machine.
And C gives us a remarkably direct way to express those operations.
The compiler uses names like x to understand which objects we're referring to. When the program executes, the machine works with memory locations, registers, and instructions. It doesn't need to remember the source-code name the way we do.
I've spent years working with software without giving much thought to the addresses of individual variables. I knew memory existed, of course. But I had never really considered how directly C allowed me to work with it.
And this brings us to our first operator.
The Two Modes of C
Or at least, that's where I used to begin.
I'd encounter an operator, memorize its meaning, and move on. Then I'd find the same symbol somewhere else, behaving differently, and suddenly everything became confusing again. An asterisk meant one thing in a declaration, something else in an expression, and apparently something entirely different inside a UNIX function.
What on earth was going on?
It turns out that my problem wasn't really pointers. My problem was how I was reading C.
C is a language, and languages have grammar. We understand this naturally when speaking English, yet somehow we forget it when programming. We memorize symbols, recognize patterns, and eventually become quite good at writing software without necessarily understanding the structure of what we're writing.
I certainly did.
When I returned to C, I began looking at its grammar differently. I started organizing what I was reading into two categories.
Declaration Mode and Expression Mode.
These aren't official modes of the language. They're my own way of recognizing two grammatical contexts, and I think they're tremendously useful.
Declaration Mode: What are we defining?
We're introducing a name and describing what it represents.
Expression Mode: What are we doing?
We're using expressions to access objects, compute values, or perform operations.
It's a simple distinction, but here's why it matters.
The same symbol can have completely different meanings depending on its grammatical context.
That's the discovery. Once I understood it, pointers became much easier to read.
Let's examine why.
Mode 1: Declaration — What Are We Defining?
Consider something familiar.
int x = 42;We've declared an integer named x and initialized it with 42. Nothing complicated, but let's look at how the declaration is constructed.
int x = 42;
│ │ │
│ │ └── Initial value
│ └────── Identifier
└────────── TypeWhile studying C, I began developing a simple reading method for declarations.
I call it TDI: Type → Description → Identifier.
It's not an official grammatical formula, but it helps me organize unfamiliar declarations. I want to know the type, understand how the declarator describes the object, and identify its name.
For an ordinary integer, there's very little to describe.
TYPE IDENTIFIER
int xBut consider this:
int *p;Now things get interesting.
TYPE DESCRIPTION IDENTIFIER
int * pWe're declaring p as a pointer to an integer.
Notice what the asterisk is doing. We're not following an address, retrieving a value, or performing an operation. We're describing what kind of variable p is.
That's Declaration Mode.
And there's a detail worth understanding. Consider:
int *p, q;We've declared two variables, but only p is a pointer. The variable q is an ordinary integer because the asterisk belongs to the declarator for p.
This is why understanding C's grammar matters. We can't simply assume that an asterisk modifies every variable in a declaration.
TDI is a useful starting point, although more complicated declarations require us to consider parentheses, arrays, functions, and how C's declarators are constructed.
But the principle remains:
Before interpreting the symbols, understand what you're declaring.
Now let's examine the other mode.
Mode 2: Expression — What Are We Doing?
Suppose we've already declared our integer.
int x = 42;The compiler knows what x represents. It knows the type, and the object has been initialized.
Now consider:
x = 100;Notice that we haven't written int again. Why would we? We're not declaring another variable; we're changing the value of an existing one.
We're in Expression Mode.
Compare these statements:
int x = 42; // Declaration with initialization
x = 100; // Assignment expressionThey look similar, but grammatically they're different. The first introduces an object and supplies its initial value. The second assigns a new value to an object that already exists.
A small difference, but a very important one.
And expressions aren't limited to assignments.
x // Refer to x
x + 10 // Compute a value
x > 50 // Compare values
x = 100 // Assign a valueAll of these are expressions. Some calculate values, some access objects, and others modify the program's state.
Here's where it becomes particularly interesting.
int y = x;This is a declaration, but the initializer contains an expression. We're introducing y and initializing it with the value of x.
Now compare:
x = 100;Here, x appears on the left side of an assignment. Instead of merely reading its value, we're identifying the object we want to modify.
An expression can help us read an object's value, but it can also identify an object whose value we want to change.
That's an important distinction, and we're going to see it again with pointers.
Notice also that our two modes can work together:
int y = x + 10;The declaration introduces y, while the expression x + 10 supplies its initial value.
int y = x + 10;
───── ──────
│ │
│ └── Expression
│
└────────── DeclarationSo we're not talking about two isolated worlds. A declaration can contain expressions, and expressions can appear in many different grammatical contexts.
The important thing is knowing which role each piece of code plays.
Now we can return to pointers with a much better understanding of the language.
A Pointer Is Still a Variable
Remember our original integer?
int x = 42;We've imagined that x lives at address 0x1000.
ADDRESS VARIABLE VALUE
──────────────────────────────────
0x1000 x 42Now suppose we want to store the address of x inside another variable.
That's what a pointer allows us to do.
int *p = &x;Let's read this using our two modes.
Declaration Mode: We're introducing p as a pointer to an integer.
Expression Mode: The initializer &x produces the address of x, which we use to initialize p.
The ampersand is simply the address-of operator here. It produces the address of an object.
That's all we need to understand about it for now.
Now imagine that p itself lives at address 0x2000.
ADDRESS VARIABLE VALUE
──────────────────────────────────
0x1000 x 42
0x2000 p 0x1000Look carefully.
x stores 42, while p stores the address of x.
Two variables, two addresses, and two different values.
A pointer is a variable whose value is an address.
That's the relationship. No mystery.
And once we understand it, something wonderful happens.
We can finally understand what the asterisk does in an expression.
The Difference Between `p` and `*p`
We've already declared our pointer.
int x = 42;
int *p = &x;Now consider:
pWe're in Expression Mode. We're referring to an existing pointer, and the value stored inside it is the address of x.
In our imaginary computer, that's 0x1000.
But now consider:
*pWe're still in Expression Mode, but the asterisk has a completely different role from the one it played in our declaration.
It's now the dereference operator.
Rather than describing a pointer, it accesses the object identified by the address stored inside that pointer.
Let's examine the relationship.
MEMORY
ADDRESS VARIABLE VALUE
──────────────────────────────────
0x1000 x 42
0x2000 p 0x1000When we evaluate p, we obtain the address 0x1000.
When we evaluate *p, we access the integer object at that address. In a context that reads its value, we get 42.
p → Address of x: 0x1000
*p → Object at 0x1000
read *p → Value of x: 42It's a remarkably straightforward relationship once you understand what the pointer contains.
Now compare these two statements.
Declaration Mode
int *p = &x;We're declaring a pointer and initializing it with an address.
Expression Mode
*p = 100;We're assigning a new value to the object reached through that pointer.
Same asterisk. Completely different grammatical role.
And this is precisely why I believe we should understand declarations and expressions before trying to memorize operators.
The grammar gives the symbol its meaning.
Changing the Pointer Versus Changing Its Destination
Here's where Expression Mode becomes particularly interesting.
Consider:
*p = 100;We're accessing the object identified by the address stored in p and assigning it a new value.
Remember our memory:
BEFORE
ADDRESS VARIABLE VALUE
──────────────────────────────────
0x1000 x 42
0x2000 p 0x1000Because p contains 0x1000, dereferencing it identifies x. The assignment therefore changes x to 100.
AFTER: *p = 100;
ADDRESS VARIABLE VALUE
──────────────────────────────────
0x1000 x 100
0x2000 p 0x1000Notice that the pointer hasn't changed. It still contains the address of x; we've simply modified the integer stored at that address.
But now consider:
p = &x;We're still in Expression Mode, but there's no asterisk. We're assigning directly to p, which means we're changing the address stored inside the pointer itself.
Let's introduce another integer.
int x = 42;
int y = 200;
int *p = &x;Our memory might look like this:
ADDRESS VARIABLE VALUE
──────────────────────────────────
0x1000 x 42
0x2000 p 0x1000
0x3000 y 200Now execute:
p = &y;We've replaced the address of x with the address of y.
ADDRESS VARIABLE VALUE
──────────────────────────────────
0x1000 x 42
0x2000 p 0x3000
0x3000 y 200Neither integer changed. We simply changed which integer the pointer refers to.
Now suppose we execute:
*p = 500;Since p now contains the address of y, this assignment modifies y.
ADDRESS VARIABLE VALUE
──────────────────────────────────
0x1000 x 42
0x2000 p 0x3000
0x3000 y 500Isn't that interesting? We changed y without writing y = 500;.
The pointer allowed us to access the object indirectly, and because we understand the relationship between addresses and expressions, we know exactly what happened.
Let's compare the three operations.
int *p = &x; // Declare and initialize p
p = &y; // Change where p points
*p = 500; // Change the object p points toThe declaration establishes what p is. The expressions determine what we do with it.
Once you understand that distinction, the code becomes considerably easier to read.
Three Declarations, Three Different Meanings
Let's examine something else that initially confused me.
Consider these three declarations independently, assuming x is an integer containing 42.
int *p = &x;We're declaring a pointer and initializing it with the address of x.
Perfectly valid.
Now:
int p = &x;This time, we're declaring an ordinary integer, but the initializer produces a pointer value.
That's a type mismatch, and C requires a compiler diagnostic.
You might argue that an address looks like a number, so why not store it inside an integer? It's a reasonable question, but C distinguishes between integers and pointers. They have different types and different rules.
Finally:
int p = x;We're declaring another integer and initializing it with the value of x, which is 42.
No pointers involved.
Now that's an important lesson.
Naming a variable p doesn't make it a pointer. Declaring it as a pointer makes it a pointer.
The compiler doesn't care what we intended the variable to represent. It follows the declaration we've written.
And that brings me to the example that started this entire investigation.
The Declaration That Started All This
I was reading UNIX documentation when I encountered this:
int pthread_rwlock_timedrdlock(
pthread_rwlock_t *restrict rwlock,
const struct timespec *restrict tsptr
);Beautiful, isn't it?
Years ago, I probably would've searched for an example and moved on. But this time I wanted to understand the declaration itself, not simply learn how to call the function.
So I started with the same question.
Declaration Mode or Expression Mode?
We're looking at a function declaration. The function is named pthread_rwlock_timedrdlock, returns an integer, and accepts two parameters.
Now let's examine the first parameter.
pthread_rwlock_t *restrict rwlockThis is where TDI becomes useful.
TYPE DESCRIPTION IDENTIFIER
pthread_rwlock_t *restrict rwlockpthread_rwlock_t is a typedef name representing a read-write lock type. The asterisk declares rwlock as a pointer, while restrict is a qualifier associated with that pointer.
There isn't some mysterious operator called *restrict. We're simply looking at separate pieces of C's declaration syntax.
Now consider the second parameter.
const struct timespec *restrict tsptrWe're declaring tsptr as a restricted pointer to a const struct timespec object. The const prevents modification of that object through this pointer, while restrict has its own rules concerning pointer-based access.
The precise meaning of restrict deserves a discussion of its own, and we'll get there.
But look at what we've accomplished.
A function declaration that initially seemed intimidating has become something we can read, examine, and understand.
And we haven't even discussed what the function actually does.
That's the beauty of understanding the grammar.
Compare:
pthread_rwlock_t *restrict rwlock;Here, the asterisk participates in a declaration.
But:
*p = 100;Here, the asterisk is an operator in an expression.
Same symbol. Different grammar. Different meaning.
Once you recognize that distinction, C becomes much less intimidating.
Learning to Read Before Learning to Memorize
Looking back, I think my biggest mistake in college was trying to memorize C without understanding how its language was constructed.
I knew that an asterisk had something to do with pointers and that an ampersand had something to do with addresses. But I treated these explanations like vocabulary definitions, expecting them to work everywhere regardless of context.
Of course, that wasn't enough.
What finally helped me was learning to ask better questions.
Am I declaring something, or am I evaluating an expression?
If I'm reading a declaration, I want to understand the type, the declarator, and the identifier. That's where TDI helps me organize what I'm seeing.
If I'm reading an expression, I want to understand what it accesses, what it evaluates to, and whether it changes anything.
These questions have made a tremendous difference. Instead of memorizing unfamiliar combinations of symbols, I can begin taking them apart and understanding how they fit together.
And that's what I find so satisfying about returning to C after all these years.
I'm not simply learning how to write programs. I'm learning how to read the language properly.
There's a very big difference.
I started this investigation thinking I needed to understand pointers, but the more I studied, the more I realized that pointers were only part of the story.
I didn't need to memorize more C. I needed to learn how to read C.
And now, some of those intimidating UNIX declarations are beginning to look rather ordinary.
Which is wonderful, because we're only getting started.
Remember something we discovered earlier: a pointer is itself a variable, and every variable lives somewhere in memory.
So here's an interesting question.
If a pointer has its own address, can another pointer point to that pointer?
Of course it can.
And now we have the grammar to understand why.