Field of struct being *const, what is the meaning of *const

What is the difference between a field being *const or just *
I couldnt find info on that.

pub const Lemming = struct
{
    game: *const Game, // backlink to game
    update_function: *const UpdateFunction,
    action: LemmingAction,
}

const UpdateFunction = fn(*Lemming) void;

this field is mutable pointer (every field of a struct is mutable in Zig, right?) to immutable data,
i’ve just written this ramblings ~47 minutes ago lol

omg. i will read…

very confusing as the syntax is now.

declaration:

update_function: *const UpdateFunction,

assignment:

self.update_function = Lemming.handle_none;

There is a ziglings exercise which explains the differences quite well. Basically for a variable (var) of type *const T, you can change the pointer to point to something else, but you can’t modify the value which it points to.

If you’re comfortable with English, you can read it from left to right and it might help: it’s a pointer (*) to a constant (const) Game. Grammatically, const is an adjective effecting Game, not *.

Note that this says nothing about whether the pointer is modifiable or not. For that, you have to look further to the left to see if it’s given a name that is var or const.

It might also help to keep in mind that a pointer is just 64 bits pointing to a memory location, and it doesn’t real care what it points to – until the type system intervenes. So changing a pointer does nothing to the struct it is pointing to.

1 Like

ok thanks, clear!
just have to get used to it.
in each and every language it is different :slight_smile: