conversion error related to array index

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Oct 4 09:11:52 UTC 2017


On Wednesday, October 04, 2017 09:04:10 thorstein via Digitalmars-d-learn 
wrote:
> Hi,
>
> I get the following compile error with this function below:
> source\mod_data\matrices.d(66,12): Error: cannot implicitly
> convert expression (j) of type ulong to uint
>
> (66,12) is [j] in row[j]
>
> Using uint as type for rows, cols (i.e. the indices) works. Is
> ulong not allowed for array indices? I could not find the
> respective information.

Array indices - and array length - are size_t. If you're on a 32-bit system,
that's an alias to uint, whereas on a 64-bit system, it's an alias to ulong.
In general, code should be using size_t when dealing with arrays not uint or
ulong (unless you're talking about having elements of uint or ulong). If you
don't use size_t, then you're likely to run into problems when compiling for
a different architecture.

But if you have a ulong that you need to convert to a uint, then you can
always cast or use std.conv.to!uint so long as the number will actually fit
in a uint.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list