The Two Jobs of the Asterisk: Pointers in Go and C++
Pointers are often confusing, and much of the reason is one detail: the asterisk means two different things depending on where it is. Here's how to read it, in Go and in C++.

Pointers confuse a lot of us the first time we meet them in Go or C++. I think most of that confusion comes from how they are written, not from the idea itself.
The idea is small. A pointer is a variable that holds a memory address, which is a number that says where some data is. You use the same idea every day. A house address is not the house. A web address is not the web page. A library shelf number is not the book.
The confusing part is one detail of the syntax. Once you see it, pointer code becomes much easier to read.
The asterisk does two different jobs, depending on where you put it.
In one place it's part of a type, like a noun. In the other it's an action, like a verb. Both Go and C++ got these two uses from C, and neither language points out the difference. This post explains the two jobs, then works through the same example in Go and in C++, then shows where the two languages really differ.
Job 1: attached to a type, the noun
When the asterisk is next to a type, it describes what kind of value a variable holds. Read it as pointer to.
// Go
var l *List // l is a "pointer to List"
func (l *List) Add(task string) { ... }
// C++
List* l; // l is a "pointer to List"
void add(List* l, std::string task) { ... }
In both languages, this tells the compiler one thing.
"I'm creating a variable named
l. Its type is pointer toList."
That's all it does. Nothing happens when this line runs. l holds a memory address. It's not a List, it's directions to one.
There is one small difference in how it's written. Go puts the asterisk before the type, as *List. C++ usually puts it after, as List*. It's the same meaning in a different word order.
Job 2: in front of a variable, the verb
When the asterisk is in front of a variable that already holds a pointer, it's an action. Read it as the value at. The proper name for this action is dereferencing.
ls := *l // Go: "the value at l"
List ls = *l; // C++: "the value at l"
Now you are giving the computer a command.
"Take the address stored in
l, go to that place in memory, and get the value there."
This is the verb. It goes somewhere and fetches something.
The third character: the ampersand
The asterisk has a partner. An ampersand, &, in front of a variable gives you that variable's address. That's how a pointer is made in the first place.
todo := List{} // a real List
l := &todo // l is now the address of todo, type *List
List todo; // a real List
List* l = &todo; // l is now the address of todo
So there are three things to recognize.
| You see | Where | Read it as | Job |
|---|---|---|---|
*List or List* | next to a type | "pointer to List" | noun, declares a type |
*l | in front of a pointer variable | "the value at l" | verb, dereference |
&todo | in front of a variable | "the address of todo" | verb, take address |
The confusing part is that the noun and the first verb use the same symbol, and they do opposite things. *List adds a layer: it means "the address of a List". *l removes a layer: it means "the value at this address".
So remember the position rule. Next to a type, the asterisk is a noun. In front of a variable, it's a verb.
Worked example: Go
Here is the Go code that confuses people most often, a method with a pointer receiver.
type List []Task
// 1. The signature: '*List' is the NOUN.
// "l is a pointer to a List."
func (l *List) Complete(id int) error {
// 2. The body: '*l' is the VERB.
// "Go to the address in l and grab the actual List."
ls := *l
for i := range ls {
if ls[i].ID == id {
ls[i].Done = true
return nil
}
}
return fmt.Errorf("no task with id %d", id)
}
There are two asterisks, four lines apart, doing two different jobs.
*List is the type. It tells Go that l holds an address. *l is the action of going to that address and reading what is stored there.
If we wrote ls := l without the asterisk, ls would be a copy of the address, which is just another pointer to the same place. With *l we get the slice itself, so we can work with the tasks in it.
Why use a pointer receiver at all? Go passes everything by value, which means every function call copies its arguments. With a value receiver, the method would get a copy of the list, add to the copy, and the caller's list would never change. A pointer receiver gives the method the address of the real list, so its changes stay.
func (l *List) Add(task Task) {
*l = append(*l, task) // write through the pointer, caller sees it
}
Go makes one thing easier. To reach a field through a pointer, you don't need the asterisk. If p is a *Task, you write p.Done, not (*p).Done, and Go adds the dereference for you. Replacing the whole value through a pointer is different. That's why Add still needs the explicit *l.
Worked example: C++
Here is the same todo list in C++, with the same two jobs.
struct Task {
int id;
bool done;
};
// 1. 'List*' is the NOUN: "l is a pointer to a List."
bool complete(std::vector<Task>* l, int id) {
// 2. '*l' is the VERB: fetch the actual vector at that address.
std::vector<Task>& ls = *l;
for (Task& t : ls) {
if (t.id == id) {
t.done = true;
return true;
}
}
return false;
}
C++ adds one more symbol, the arrow, ->. Go follows the pointer for you when you access a field. C++ makes you write it, either as (*p).done or with the shorter p->done. Read the arrow as "follow the pointer, then take the field".
Task* p = &todo;
(*p).done = true; // verb, then field: clunky
p->done = true; // the same thing, and what everyone writes
The declaration gotcha C++ inherited from C
C's designers wanted a declaration to look like the way the variable is used. So int *p means "*p is an int". It's a neat idea, but in practice it causes a trap, because the asterisk belongs to the variable name, not to the type.
int* a, b; // a is int*, but b is just int
int *a, *b; // two pointers, asterisk repeated per name
Go's designers saw this trap cause bugs for decades, so Go's types read from left to right instead. In Go, var a, b *int makes two pointers, and there is no other way to read it. In C++, the usual style is one declaration per line, which avoids the trap. The full history of this debate has its own post.
Where the languages actually differ
The reading rules above are the same in both languages. The safety rules are not.
| Go | C++ | |
|---|---|---|
| Pointer arithmetic | forbidden | allowed, so p + 1 moves to the next element |
| Null pointer | nil | nullptr |
| Freeing memory | garbage collector | your job, through delete or RAII |
| Dangling pointers | can't happen, the GC keeps referents alive | a common source of bugs |
| Field access via pointer | p.field, auto-dereferenced | p->field |
| Alternative to pointers | none needed | references, which can't be null or reseated |
Three of these rows need more explanation.
Pointer arithmetic means adding a number to a pointer to move it through memory. It makes C++ pointers more powerful and more dangerous. *(p + 3) reads the fourth element of an array. If you got the array's length wrong, it reads whatever happens to be stored after the array. Go doesn't allow pointer arithmetic at all. To get the fourth element you index a slice, and Go checks that the index is inside the slice.
Lifetimes are the biggest difference. A variable's lifetime is how long its memory stays valid. In C++, returning the address of a local variable is a bug. The function's stack frame is removed when it returns, so the pointer points at memory that now holds something else. In Go, the same code is normal. The compiler notices that the address is used after the function returns, and it moves the variable to the heap. This check is called escape analysis. The code looks the same in both languages, and the result is the opposite.
func newTask(id int) *Task {
t := Task{ID: id}
return &t // fine in Go, t escapes to the heap
}
Task* newTask(int id) {
Task t{id};
return &t; // undefined behaviour in C++, t is gone
}
References are the C++ way to share an object without pointer syntax. A List& parameter works like the object itself. There is no asterisk, no arrow, and it can't be null. Modern C++ uses references in the places where Go would use a pointer receiver. It keeps plain pointers for things that might be missing or that the code doesn't own, and uses references and smart pointers everywhere else.
The cheat sheet, one more time
- An asterisk next to a type is a noun. It means "pointer to". It only declares.
- An asterisk in front of a pointer variable is a verb. It means "the value at". It goes to the address and fetches.
- An ampersand in front of a variable is a verb. It means "the address of". It's how pointers are made.
p.fieldin Go andp->fieldin C++ do the same thing. They follow the pointer and take the field.- Copying a pointer copies the address, not the data. Dereferencing is the only way to reach the data.
When a line full of pointers is hard to read, read it as words, from left to right. func (l *List) is "l, a pointer to List". ls := *l is "ls gets the value at l". The syntax is short, but each piece only ever says one of those three things.
What an address physically is, and why the stack and the heap treat these values so differently, is covered in Memory: The Stack, the Heap, and Pointers.
Check yourself
3 questions · nothing is saved- Written
- Apr 8, 2026
- Length
- 1,836 words · 8 min
- Built
- …
- Served by
- …
- Delivered
- …
- Over the wire
- …
- Go's share
- …
List* l or List *l? The Oldest Style War in Programming
Three spellings that mean the same thing to the compiler. People have argued about where the asterisk goes since the 1970s, because the question is really about what a declaration is. Here's the whole debate, and how Go avoided it.
- 1C++ 101: The Language That Trusts You Completely
- 2Go 101: A Small Language for Big Systems
- 3The Two Jobs of the Asterisk: Pointers in Go and C++
- 4List* l or List *l? The Oldest Style War in Programming
- 5From C++ to Go: Demystifying Pointers, Slices, and Errors
- 6Why Go Rewrites 0644 as 0o644 (and Why chmod Won't Take 'rwx')
Memory: The Stack, the Heap, and Pointers
Where your variables actually live, why the heap costs more than the stack, and what a pointer really is.
OpenCV 5: What Actually Changed Under the Hood
The first major version since 2018 is about foundations, not new features: a rebuilt engine that runs over 80% of ONNX instead of 22%, real n-dimensional arrays, fast code from four chip makers, and a list of removals long enough to break your build.
OpenCV 101: Every Image Is a Grid of Numbers
A photo is a rectangle of numbers, and everything OpenCV does is arithmetic on that rectangle. Mat, BGR, convolution, thresholds, contours and perspective warps: the ideas that make the library's 2,500 functions easier to find your way around.
Projects built with what this post is about.


