我剛開始學習有關數組作爲函數參數,我似乎不能包圍我的頭。我試圖創建一個函數,要求用戶輸入矩陣的值,然後將矩陣讀回給他們。不過,我似乎無法讓它工作,下面是我的一個嘗試的例子。任何人都可以幫我弄清楚如何讓它起作用嗎?謝謝。試圖找出數組作爲函數參數
#include<iostream>
#include<cstdlib>
using namespace std;
void fillinvalues(int m[4]){
cout<<"Enter the first element in your matrix\n";
cin>>m[0];
cout<<"Enter the second element in your matrix \n";
cin>>m[1];
cout<<"Enter the third element in your matrix \n";
cin>>m[2];
cout<<"Enter the fourth element in your matrix \n";
cin>> m[3];
cout<<m[0]<<","<<m[1]<<"\n"<<m[2]<<","<<m[3];
}
int main(){
fillinvalues();
return 0;
}
「m」數組不是奇蹟般地創建,你需要傳遞一個函數n – 2014-09-30 20:31:31
fillinvalues需要一個大小爲4的整型數組的輸入。不幸的是,當你調用main函數時,你沒有提供任何東西! – Decipher 2014-09-30 20:32:19
@Decipher但請注意,它將接受任何指向'int'的指針。 – juanchopanza 2014-09-30 20:35:44