博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
传入一维数组到函数 Passing 1D array to function
阅读量:4505 次
发布时间:2019-06-08

本文共 1082 字,大约阅读时间需要 3 分钟。

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:

#include 
using namespace std;int SumValues (int [], int ); //function prototypevoid 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 “ <

 

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 
using namespace std;int SumValues (const int [], int); //function prototypeint 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 “ <

 

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

转载于:https://www.cnblogs.com/learnopencad/p/4314724.html

你可能感兴趣的文章
Test指令
查看>>
c++11——可变参数模板
查看>>
from imp import * 重新加载导入的模块reload
查看>>
二叉树三种遍历调试运行版
查看>>
关于PHP、python使用的CRC32函数
查看>>
JS自动关闭授权弹窗,并刷新父页面
查看>>
c#语言几种常见循环代码
查看>>
SQL多表连接查询(详细实例)
查看>>
Http中涉及到的知识点总结
查看>>
测试计划
查看>>
adb命令记录
查看>>
Ecstore Nginx Rewrite(去掉链接中的index.php) ECSTORE 伪静态
查看>>
Dash
查看>>
BZOJ 1876: [SDOI2009]SuperGCD
查看>>
swift初学日志
查看>>
CCF真题之出现次数最多的数
查看>>
Eclipse上GIT插件_客户端配置
查看>>
使用HANA Web-based Development Workbench创建最简单的Server Side JavaScript
查看>>
JavaScript浏览器对象之二Document对象
查看>>
算法导论 第二部分——排序和顺序统计量
查看>>