yohhoyの日記

技術的メモをしていきたい日記

関数パラメータリストと配列型

C/C++言語における関数パラメータリスト中の配列型に関するメモ。

下記の関数f1, f2はどちらの宣言でも同じ関数(同一の型)として扱われる。

void f1(int a[42], const char b[]);
void f1(int * a, const char * b);
// a = int型へのポインタ型, b = const修飾int型へのポインタ型

void f2(double c[2][3][4]);
void f2(double (*c)[3][4]);
// c = '"double型の4要素配列"の3要素配列'へのポインタ型

C

C99(JTC1/SC22/WG14 N1256) 6.7.5.3/p7より該当箇所を引用。

A declaration of a parameter as "array of type" shall be adjusted to "qualified pointer to type", where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation. (snip)

後半の"those specified within the [ and ] ..."はC99からの新機能を指す*1。例:void f(int a[const])void f(int * const a)と等しい。

C++

C++03 8.3.5/p3より該当箇所を引用。

(snip) The type of a function is determined using the following rules. The type of each parameter is determined from its own decl-specifier-seq and declarator. After determining the type of each parameter, any parameter of type "array of T" or "function returning T" is adjusted to be "pointer to T" or "pointer to function returning T," respectively. (snip)