传入一维数组到函数 Passing 1D array to function
2015-03-05 01:07
120 查看
Since arrays are always passed by reference, all changes made to the array elements inside the function will be made to the original array.
int ProcessValues (int [], int ); // works with ANY 1-d array
Example:
The only way to protect the elements of the array from being inadvertently changed, is to declare an array to be a const parameter.
Reference:
http://stackoverflow.com/questions/23035407/passing-1d-and-2d-arrays-by-reference-in-c http://doursat.free.fr/docs/CS135_S06/CS135_S06_8_1D_Arrays2.pdf
int ProcessValues (int [], int ); // works with ANY 1-d array
Example:
#include <iostream> using namespace std; int SumValues (int [], int ); //function prototype void main( ) { int Array[10]={0,1,2,3,4,5,6,7,8,9}; int total_sum; total_sum = SumValues (Array, 10); //function call cout <<”Total sum is “ <<total_sum; } int SumValues (int values[], int num_of_values) //function header { int sum = 0; for( int i=0; i < num_of_values; i++) sum+=values[i]; return sum; }
The only way to protect the elements of the array from being inadvertently changed, is to declare an array to be a const parameter.
#include <iostream> using namespace std; int SumValues (const int [], int); //function prototype int main( ) { const int length =10; int Array[10]={0,1,2,3,4,5,6,7,8,9}; int total_sum; total_sum = SumValues (Array, length); //function call cout <<”Total sum is “ <<total_sum; return 0; } int SumValues (const int values[], int num_of_values) //function header { int sum = 0; for( int i=0; i < num_of_values; i++) sum+=values[i]; return sum; }
Reference:
http://stackoverflow.com/questions/23035407/passing-1d-and-2d-arrays-by-reference-in-c http://doursat.free.fr/docs/CS135_S06/CS135_S06_8_1D_Arrays2.pdf
相关文章推荐
- 一维数组的函数参数传递
- php分享一个n维数组转一维的函数
- 个有10个元素的整型一维数组,用户输入9个数据,调用函数,对数组元素进行从小到大排序后,在函数中输入一个数,插入到数组中正确的位置,并输出
- c语言一维数组做参数传递给函数:
- 【CVTE】请使用js编写一个函数:chunk(arr,size),把一个数组arr按照指定的数组大小size分割成若干个数组块,传入的参数分别为数组
- 设计一个函数:将一维整型数组中的元素逆序存放
- Linux 命令精选--如何将数组传入到函数中
- c语言一维数组做参数传递给函数:
- oj3117复杂类型作函数参数之一维数组作函数参数
- c_指针_一维数组名作为函数参数
- js将多维数组转换为一维数组的函数
- 利用递归把多维数组转为一维数组的函数
- 数组 传入 函数
- 虚幻引擎CPP函数传入数组有时候编译不过的bug。
- 编写一个函数,传入一个int型数组,返回该数组能否分成两组
- 二级指针、数组指针、二维数组、指针数组作为函数形参时可传入的实参
- 一维数组和二位数组作为函数参数进行传递的方式
- array_column() 函数[二维数组转为一维数组]
- 如何把多维数组传入函数做为参数之几个方法
- a[9]与9[a]的确是相同的,以及函数内部如何得到传入数组参数的大小