Passing in a one-dimensional array to a function passing 1D array to function

Source: Internet
Author: User

Since arrays is always passed by reference, all changes made to the array elements inside the function would be made to th E original Array.

int processvalues (int [], int); Works with any 1-d array

Example:

#include <iostream>using namespacestd;intSumvalues (int[],int);//function prototypevoidMain () {intarray[Ten]={0,1,2,3,4,5,6,7,8,9}; inttotal_sum; Total_sum= Sumvalues (Array,Ten);//function Callcout << "Total sum is"<<total_sum;}intSumvalues (intValues[],intNum_of_values)//function Header{  intsum =0;  for(intI=0; i < num_of_values; i++) Sum+=Values[i]; returnsum;}

The only-to-protect the elements of the array from being inadvertently changed, are to declare an array to be a const p Arameter.

#include <iostream>using namespacestd;intSumvalues (Const int[],int);//function prototypeintMain () {Const intLength =Ten; intarray[Ten]={0,1,2,3,4,5,6,7,8,9}; inttotal_sum; Total_sum= Sumvalues (Array, length);//function Callcout << "Total sum is"<<total_sum; return 0;}intSumvalues (Const intValues[],intNum_of_values)//function Header{  intsum =0;  for(intI=0; i < num_of_values; i++) Sum+=Values[i]; returnsum;}

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

Passing in a one-dimensional array to a function passing 1D array to function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.