タイトルの通り2次元配列で作ったものを関数の引数として渡したいです。また、2次元配列の大きさは固定ではありません。
私が書いたコードは、以下のようになります。
C++
1#include<iostream> 2 3using namespace std; 4 5void array(int n,int a[][n]){ 6 for(int i = 0;i < n;i++){ 7 for(int j = 0;j < n;j++){ 8 a[i][j] = i*j; 9 } 10 } 11 12} 13 14int main(){ 15 int n; 16 cin >> n; 17 int a[n][n]; 18 array(n,a); 19 20} 21
この状態で実行すると、以下のようなエラーが出ます。
g++ test.cpp
test.cpp:5:27: error: use of parameter outside function body before ']' token
void array(int n,int a[][n]){
^
test.cpp: In function 'void array(...)':
test.cpp:6:21: error: 'n' was not declared in this scope
for(int i = 0;i < n;i++){
^
test.cpp:8:7: error: 'a' was not declared in this scope
a[i][j] = i*j;
理由が分かる方教えてくれると助かります。
回答5件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。