Multidimensional array
Oleksiy
oleksiy.pavlyuk at gmail.com
Fri Jul 5 21:00:20 PDT 2013
> However, that is a confusing syntax because the right-hand side
> is not the same type as the elements, which is dchar[3].
> Perhaps D supports it for C compatibility?
Yes, I noticed:
arr = '!';
Error: cannot implicitly convert expression ('!') of type char to
dchar[3u][]
Look like there is a consequence - can't perform array-wise
operations on multidimensional arrays? And that's why a need to
touch the elements themselves, like in the example you provided:
import std.stdio;
>
> void main()
> {
> dchar[3][5] arr = '.';
>
> foreach (ref e; arr) {
> e[2] = '!';
> }
>
> writeln(arr);
> }
> a[] alone is a slice to all of the elements of 'a'. When you
> use that syntax in an operation, then that operation is applied
> "array-wise":
>
> a[] = b[] + c[];
>
> That is the same as
>
> a[i] = b[i] + c[i]
>
> for all valid values of 'i'.
I truly got what the problem is after this part. Thanks for a
very detailed explanation.
Also huge thank you for your book - holy grail for beginners.
--------------------------------
> But this doesn't compile:
>
> char[3][5] arr = [ '.', '.', '.' ];
>
> Error: mismatched array lengths, 15 and 3
>
> I see that as a bug but can't be sure.
Others seem to agree with you, will you be submitting this bug?
--Oleksiy
More information about the Digitalmars-d-learn
mailing list