LoginSignup
2
2

More than 5 years have passed since last update.

Dでの多次元配列の宣言による行/列の扱いの違い

Posted at

多次元配列を宣言する際、

int[10][5] arr;

# arr => 
# [0 0 0 0 0 0 0 0 0 0]
# [0 0 0 0 0 0 0 0 0 0]
# [0 0 0 0 0 0 0 0 0 0]
# [0 0 0 0 0 0 0 0 0 0]
# [0 0 0 0 0 0 0 0 0 0]

のように宣言すると5行10列の(固定サイズ)配列になるが、

auto arr = new int[][](10, 5);

# arr =>
# [0 0 0 0 0]
# [0 0 0 0 0]
# [0 0 0 0 0]
# [0 0 0 0 0]
# [0 0 0 0 0]
# [0 0 0 0 0]
# [0 0 0 0 0]
# [0 0 0 0 0]
# [0 0 0 0 0]
# [0 0 0 0 0]

のように宣言すると10行5列の(動的サイズ)配列になる。

配列アクセスは
arr[i][j] # => i行j列目
なので、後者の形で宣言した方がバインド順の逆転が起こらない。

プログラミング言語Dの4.3節を読んでいて気になったが、ずっと後者の形を使っていたので逆転があることに気付かなかった……。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2