//_7_5_main.cpp //传递数组和单个数组元素到函数中 //传递整个数组时是引用传递,对引用的任何修改都会修改原数组, //传递某个数组元素时是直接传值调用,,,, #include <iostream> #include <iomanip> using namespace std; void modifyArray(int [] ,int);//传递整个数组 void modifyElement(int);//传递数组元素 int main() { const int arraySize = 5; //调用一个const类型的数组时不可改变其中的数组元素值, //否则就会出现编译错误!!!! int a[arraySize] = {1,2,3,4,5}; //打印没有引用之前的数组 cout << "Effects of passing entire array by reference:" << "\nThe values of the original array are :" << endl; for(int k=0;k<arraySize;k++) cout << setw(3) << a[k]; //引用整个数组 modifyArray(a,arraySize); //打印引用之后的数组 cout << "\nThe values of the modified array are:" << endl; for(int n=0;n<arraySize;n++) cout << setw(3) << a[n]; cout << "\n\nEffects of passing element by value:" << "\na[3] before modifyElement :" << a[3] << endl; modifyElement(a[3]); cout << "a[3] after modifyElement :" << a[3] << endl; system("pause>>cout"); return 0; } void modifyArray(int b[],int sizeOfArray) { for(int i=0;i<sizeOfArray;i++) b[i]*=2; } void modifyElement(int e) { cout << "Value of element in modifyElement:" << (e*=2) << endl; }
C++大学基础教程_7_5将数组传递给函数
猜你喜欢
转载自jia-shun.iteye.com/blog/2067685
今日推荐
周排行