L5 · LanguagesProgramming Languages 101 · part 3 of 6

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++.

Takumi Yamada
· 8 min read
pointersgocppmemoryprogramming-languages
The C++ logo, a blue hexagon with the letter C, next to the Go logo, the word GO in light blue.
C++ and Go both write pointers with an asterisk, and both got that from C.Left, logo: Jeremy Kratz for the Standard C++ Foundation, public domain (trademark), via Wikimedia Commons (original). Changes: placed on a background.Right, logo: The Go Authors, public domain (trademark), via Wikimedia Commons (original). Changes: placed on a background.

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 , 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 one thing.

"I'm creating a variable named l. Its type is pointer to List."

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.

0xc000012340l: an address*ltodothe List at that addresscopiedlsthe value at l
The verb: follow the address in l and take the value found there.

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 seeWhereRead it asJob
*List or List*next to a type"pointer to List"noun, declares a type
*lin front of a pointer variable"the value at l"verb,
&todoin front of a variable"the address of todo"verb, take address
ltype *Listaddress of todo*List next to a type is the noun:pointer to Listtodotype ListA REAL LIST*lthe value at l&todothe address of todo
The three rows of that table on one picture. The type says what l holds, and the two verbs go in opposite directions.

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 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
}
VALUE RECEIVERfunc (l List) Add(task Task)callerT0T1T2copiedmethodT0T1T2T3the new task lands in the copy,and the caller still has threePOINTER RECEIVERfunc (l *List) Add(task Task)callerT0T1T2T3methodl*l = append(*l, task)the write goes through the address,so the caller's list has four
The same Add method with each kind of receiver. T3 is the task being added.

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.

GoC++
Pointer arithmeticforbiddenallowed, so p + 1 moves to the next element
Null pointernilnullptr
Freeing memoryyour job, through delete or
Dangling pointerscan't happen, the GC keeps referents alivea common source of bugs
Field access via pointerp.field, auto-dereferencedp->field
Alternative to pointersnone neededreferences, which can't be 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 . 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.

memoryp1stp + 12ndp + 23rdp + 34thp + 4?an array of fourpast the end
In C++, p + 3 is the fourth element of this array, and p + 4 is whatever comes after it.

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 . 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
}
GOt is moved to the heapCALLERpPOPPEDHEAPtthe pointer still leads to tC++t stays in the frameCALLERpPOPPEDtt is gonethe pointer leads into a framethat no longer exists
What the caller's pointer leads to after newTask has returned.

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.field in Go and p->field in 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
  1. 1.In func (l *List) Add(task Task), what job is the asterisk in *List doing?
  2. 2.In Go, p is a *Task. How do you set its Done field?
  3. 3.A function returns &t, the address of its own local variable t. What happens?

Who wrote this

Author profile and background

Takumi Yamada

Takumi Yamada

· Backend Engineer

Backend engineer building services, databases, Linux tooling, and NEGEN SYSTEMS. Writing about low-level mechanics, distributed systems, and real-world security.

End of post
NEGEN SYSTEMS
Receipt · post 17 of 31
Written
Apr 8, 2026
Length
1,836 words · 8 min
Built
…
Served by
…
Delivered
…
Over the wire
…
Go's share
…
Next in Programming Languages 101 · part 4 of 6

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.

8 min read
From other series