わさっきhb

大学(教育研究)とか ,親馬鹿とか,和歌山とか,とか,とか.

K&Rでは,配列のi番目の要素はthe i-th element

こんな感じでプログラミング解説で「N番目」という言葉が出てきたら、プログラマは脳内で無意識に「0起点かな、1起点かな」と考えさせられる。脳味噌のCPUタイムの無駄である(笑)それでも、経験的にプログラミング解説では0起点の「N番目」が使われていることが多いようだ。

配列の先頭要素が「0番目」であることは気持ち悪いか…「N番目」という言葉を考察してみる - http://rubikitch.com/に移転しました

「0起点・1起点」は,「0オリジン・1オリジン」として,むかし調べたことがあります*1

ともあれ,ブックマークで書いたとおり,"C Programming Language (Prentice Hall Software)" に当たってみました.太字は引用者によるものです.

The declaration
a[10];
defines an array a of size 10, that is, a block of 10 consecutive objects named a[0], a[1], ..., a[9].
(図:省略)
The notation a[i] refers to the i-th element of the array.
(pp.97-98)

a[i]を「配列のi番目の要素」と言い切っています.見た限り,「0起点」に相当する表現はありませんでした.
K&Rでは『配列の先頭要素が「0番目」』についてどんな表現をとっているか?」という疑問に対する答えは,上の英文でおしまいです.あとは,関係しそうなところを抜き出していきます.

int *pa;
(略)
pa = &a[0];
(略)
If pa points to a particular element of an array, then by definition pa+1 points to the next element, pa+i points i elements after pa, and pa-i points i elements before.
(p.98)

ポインタでは,「paのi個後ろの要素を指す」という表現になって,「i-th」が見当たりません…

The meaning of "adding 1 to a pointer," and by extension, all pointer arithmetic, is that pa+1 points to the next object, and pa+i points to the i-th object beyond pa.
(pp.98-99)

あ,出てきました.

Applying the operator & to both parts of this equivalence, it follows that &a[i] and a+i are also identical: a+i is the address of the i-th element beyond a.
(p.99)

これは&a[i]とa+1の等価性の解説です.

Perhaps the most common occurrence of string constants is as arguments to functions, as in
printf("hello, world\n");
When a character string like this appears in a program, access to it is through a character pointer; printf receives a pointer to the beginning of the character array. That is, a string constant is accessed by a pointer to its first element.
(p.104)

ここに「first」が出てきますが,「1番目の」ではなく,「最初の」という意味ととらえればよさそうです.

以下余談.

There is one difference between an array name and a pointer that must be kept in mind. A pointer is a variable, so pa=a and pa++ are legal. But an array name is not a variable; constructions like a=pa and a++ are illegal.
(p.99)

pointerは,自分の授業でいう「ポインタ変数」に,array nameが「配列変数」に相当します.「ポインタ値」に相当する表現はなさそうです.

*1:部分文字列を得る方法がbashとzshで違う - わさっき.本文よりも脚注のリンクをご覧ください.