# Mutable and Constant Pointer Semantics

**URL:** <https://ziggit.dev/t/mutable-and-constant-pointer-semantics/3172>\
**Category:** Docs\
**Tags:** language\
**Created:** [February 8, 2024, 10:04am UTC](https://ziggit.dev/t/mutable-and-constant-pointer-semantics/3172 "2024-02-08T10:04:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [February 8, 2024, 10:04am UTC](https://ziggit.dev/t/mutable-and-constant-pointer-semantics/3172/1 "2024-02-08T10:04:07Z")

</div>

Pointers carry many distinct characteristics. One important characteristic is the mutability via the `const` keyword which controls how addresses and data can be mutated.

There are 4 basic `const` combinations as it relates to pointers.

```zig
var x: *T // x is a mutable address to mutable data.
const x: *T // x is a constant address to mutable data.
var x: *const T // x is a mutable address to constant data.
const x: *const T // x is a constant address to constant data.

```

Much like a physical location, a valid pointer refers to a region that has been denoted with an address. In other words, we can find that region by following its address (much like locating a monument using coordinates).

The dual `const` qualification means we can either change the address, or change the thing at the address, or some combination of the two.

### Example 1: Constant address to mutable data

```zig
var x: usize = 42;
var y: usize = 43;

const ptr = &x; // the address is const, but the data is not.

ptr = &y; // this fails because we cannot change the address.

ptr.* += 1; // this succeeds because we can change the data.

```

### Example 2: Mutable address to constant data

```zig
const x: usize = 42;
const y: usize = 43;

var ptr = &x; // the data is const, but the address is not.

ptr = &y; // this succeeds because we can change the address.

ptr.* += 1; // this fails because we cannot change the data.

```

### Example 3: Mutable address to mutable data

```zig
var x: usize = 42;
var y: usize = 43;

var ptr = &x; // both the data and the address are mutable.

ptr = &y; // this succeeds because we can change the address.

ptr.* += 1; // this succeeds because we can change the data.

```

### Example 4: Constant address to constant data

```zig
const x: usize = 42;
const y: usize = 43;

const ptr = &x; // both the data and the address are constant.

ptr = &y; // this fails because we cannot change the address.

ptr.* += 1; // this fails because we cannot change the data.

```

### Explicitly declaring mutability

Mutable data can be _viewed_ as constant by using the `const` keyword on the pointer explicitly.

```zig
// the data itself is mutable
var x: usize = 42;

// the pointer sees the data as const
const ptr: *const usize = &x;

```

Note that this follows typical rules surrounding `const` - `const` qualification cannot be dropped without using `@constCast`.

```zig
// the data itself is const
const x: usize = 42;

// this fails because the data is const
const ptr: *usize = &x;

```

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [February 8, 2024, 10:35am UTC](https://ziggit.dev/t/mutable-and-constant-pointer-semantics/3172/2 "2024-02-08T10:35:58Z")

</div>

> [@AndrewCodeDev](#):
>
> The dual `const` qualification means we can either change the address, or change the thing at the address, or some combination of the two

I’m a bit confused. What did you mean here exactly? Did you mean

- dual _nature_ (can appear both before `:` and after `:`) of `const` qualifier  
or
- `const x: *const T` case, double `const`  
?

---

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [February 8, 2024, 10:37am UTC](https://ziggit.dev/t/mutable-and-constant-pointer-semantics/3172/3 "2024-02-08T10:37:37Z")

</div>

Sure - good question.

By dual const (dual meaning two of something), I mean that const can show up twice in a single type declaration. Now, we know that you can have more than two consts…

`const x: []const []const u8` is 3 qualifiers and denotes a constant slice to constant slices of u8’s. The point of this Doc though is to give a basic understanding of the most common pointer qualifiers people encounter (especially in the beginning).

`const _: *const _`

Do you have a suggestion to make this more clear?

Also, we could edit this article to add more content too for more advanced cases -  
`Docs` are open ended after all.

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [February 8, 2024, 11:27am UTC](https://ziggit.dev/t/mutable-and-constant-pointer-semantics/3172/4 "2024-02-08T11:27:43Z")

</div>

> [@AndrewCodeDev](#):
>
> Do you have a suggestion to make this more clear?

Well, English is not my native… ok, I’ll try…  
“As we can see from the examples above, `const` qualifier can be used on both sides of `:` when declaring pointers. When it is used on the left side, before a variable (argh…) name (`const ptr : ...`), it denotes immutability of the pointer itself, and when it is used on the right side (`... : *const T;`), it denotes immutability of data pointed to by that pointer”.

something like this 🙂

---

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [February 8, 2024, 1:57pm UTC](https://ziggit.dev/t/mutable-and-constant-pointer-semantics/3172/5 "2024-02-08T13:57:33Z")

</div>

Thanks! I’ll get this formatted and included tomorrow.

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [February 11, 2024, 3:03pm UTC](https://ziggit.dev/t/mutable-and-constant-pointer-semantics/3172/6 "2024-02-11T15:03:37Z")

</div>

4 posts were split to a new topic: [Alternative declaration syntax](https://ziggit.dev/t/alternative-declaration-syntax/3218)
