Two ways to pass an array to C + + functions

Source: Internet
Author: User

Array with pointers.

The header addresses the past and then outputs the array elements through the address.

1. One-dimensional arrays

#include <iostream>using namespacestd; #include<cstring>voidFarray1 (intArray1[],intLen//Note: There is no way to get the length through array1, only by passing the parameter, because it is the first address of the array. {     for(intI=0; i<len;i++)    {        //cout<<array1[i]<<endl;cout<<* (array1+i) <<endl;//because the first address is passed, both methods can output array elements    }}voidFarray2 (int*array1,intLen//Note: There is no way to get the length through array1, only by passing the parameter, because it is the first address of the array. {     for(intI=0; i<len;i++)    {        //cout<<array1[i]<<endl;cout<<* (array1+i) <<endl;//because the first address is passed, both methods can output array elements    }}voidMain () {intmarks[5] = { +, -, the,Bayi, *}; intlength1=sizeof(marks)/4;  Farray1 (MARKS,LENGTH1);  Farray2 (MARKS,LENGTH1); System ("Pause");}

2, two-dimensional array:

#include <iostream>using namespacestd; #include<cstring>voidFarray1 (intarray1[][3],intLen1,intLEN2)//Note: There is no way to get the length through array1, only by passing the parameter, because it is the first address of the array. {     for(intI=0; i<len1;i++)    {         for(intj=0; j<len2;j++)        //cout<<array1[i][j]<<endl;cout<<* (* (array1+i) +j) <<endl;//because the first address is passed, both methods can output array elements    }}voidFarray2 (int(*array1) [3],intLen1,intLEN2)//Note: To specify the number of array columns, you cannot pass{     for(intI=0; i<len1;i++)    {         for(intj=0; j<len2;j++)        //cout<<array1[i][j]<<endl;cout<<* (* (array1+i) +j) <<endl;//because the first address is passed, both methods can output array elements    }}voidMain () {intmarks[2][3] = {{ +, -, the},{Bayi, *, About}}; intlength1=sizeof(marks)/4;//get the total length of the array  intLength2=sizeof(marks[0])/4;//get number of columns  intLength3=length1/length2;//get the number of rowsfarray1 (marks,length3,length2);  Farray2 (MARKS,LENGTH3,LENGTH2); System ("Pause");}

Two ways to pass an array to C + + functions

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.