3

重複の可能性:
C++ポインターと参照トピックに関する混乱

私が通過しているとしましょう

int arr[10];

関数のパラメータとして。

これらの有効な関数プロトタイプはすべてありますか?それらは議論の点でどのように異なりますか、そしてその理由は何ですか?

これは私がこれまでに知っていることです(正しいかどうかはわかりません)

1. void foo(int &arr);      //a reference to an array, which preserve the size attribute?
2. void foo(int &arr[]);    //the same (is it)?
3. void foo(int (&arr)[]);  //I don't know
4. void foo(int &arr[10]);  //is it the same as 2?
5. void foo(int (&arr)[10]);//no idea

6. void foo(int *arr);      //a decayed pointer of the array, pointing to the first element of the array, losing the size attribute?

7. void foo(int *arr[]);    //the same (is it?)
8. void foo(int (*arr)[]);  //a pointer to an array, preserve the size
9. void foo(int *arr[10]);  //the same as 7 (is it?)
10. void foo(int (*arr)[10]);//is the same as 8 (is it?)

11. void foo(int arr[]);    //is the same as 6 (is it?)
12. void foo(int arr[10]);  // is the same as 6 (is it?)

(これには長い説明が必要になることを私は知っています、申し訳ありませんが、私は完全に混乱しています...)

4

2 に答える 2

8

最初の重要な情報は、型が (制限付きまたは制限なし) の配列であるパラメーターは、へのTポインターに変換されるということTです。つまり、int arr[]との両方int arr[10]が に変換されint * arrます。変換は最上位の配列でのみ実行されることに注意してください。つまりint (*arr)[10]、int の配列へのポインタである では発生しません。

さらに、識別子の右側にあるものは、左側にあるものよりも密接にバインドされます。つまりint *arr[10]、 は配列ですint (*arr)[10]が、 はポインターです。

最後に、参照の配列および参照へのポインターは無効であり、無制限の配列へのポインターおよび参照も同様です。

1. void foo(int &arr);        // can't pass, reference to int
2. void foo(int &arr[]);      // invalid, pointer to reference to int
3. void foo(int (&arr)[]);    // invalid, reference to unbounded array of int
4. void foo(int &arr[10]);    // invalid, pointer to reference to int
5. void foo(int (&arr)[10]);  // can pass, reference to an array of int

6. void foo(int *arr);        // can pass, pointer to int
7. void foo(int *arr[]);      // can't pass, pointer to pointer to int
8. void foo(int (*arr)[]);    // invalid, pointer to an unbounded array of int.
9. void foo(int *arr[10]);    // can't pass, pointer to pointer to int
10. void foo(int (*arr)[10]); // can't pass, pointer to array of int

11. void foo(int arr[]);      // can pass, pointer to int
12. void foo(int arr[10]);    // can pass, same as above

arr引数として toを使用fooすると、最初の要素へのポインターに減衰します。渡される値fooは型になりint *ます。&arr数値 10 に渡すことができることに注意してください。この場合、 type の値int (*)[10]が渡され、減衰は発生しません。

于 2012-10-17T17:27:46.980 に答える
0

難しい部分は、配列が値渡しではなく、ポインターに崩壊することを考慮することです。

宣言の一部は構文エラーであり、他の宣言はそうではありません(ただし、おそらくあなたが考えているものでもありません)

あなたの場合、意味のあるのは 6、11、12 だけです。

2、3、4、および 8 には、自明のエラー メッセージがあります。(それらを理解していない場合は、間違った演算子の優先順位で宣言を読んだことが原因である可能性が最も高いです)

t1.cpp:2:19: error: declaration of 'arr' as array of references
t1.cpp:3:22: error: parameter 'arr' includes reference to array of unknown bound 'int []'
t1.cpp:4:21: error: declaration of 'arr' as array of references
t1.cpp:8:22: error: parameter 'arr' includes pointer to array of unknown bound 'int []'

他のものはどういうわけか冗長です(配列への参照または-ポインター-、関数内で同じように動作します)または意図したとおりに異なるものを宣言するため(7、9、10のように:それらは「二重間接」を表します)、単に間違っています、一方、プレーンな配列には単一の配列があり、 1. は間接を表していません: 単一の int をエイリアスするだけです)

于 2012-10-17T17:42:39.543 に答える