Confusion considering the types of energy

Hi,
while debugging i found something which confuses me.
In the function calculate_min_energy_column energy is of type uint32_t const * const.
How does one acces the values of energy in that case.
And why is it of different type?
In calculate_energy, energy is of type uint32_t* const, where you can exess the values easily by energy[index].
Thanks in advance.

Hi Konstantin,

the declaration uint32_t* const energy can be read as “a constant pointer to a uint32_t”, while the declaration uint32_t const* const energy can be understood as “a constant pointer to a constant uint32_t”.

The key difference here is whether you can write into the container referenced by the pointer or not.
In the first case, the pointer points to a normal uint32_t, so you can modify the associated container.
In the second case, the underlying uint32_t const is constant, meaning you can not modify it through the pointer.
In both cases, the pointer itself is constant, which simply means you are not allowed to re-assign the pointer.

This makes sense because in the calculate_energy function, you need to overwrite the contents of the array, while calculate_min_energy_path only needs to read values from the array, not write into it.

In any case, energy points to an array, so you can access the values in the same fashion in both cases, via the subscript operator [].

Does this answer your question?

Lucas

2 Likes

Yes it does, thank you.