[C++ Primer] Passing an array to a function by reference/pointers

#include<cstdlib>

#include
<iostream>



usingnamespacestd;



//Takes a pointer to the array, and the size of the array.

voidprint_arr1(constintarr, size_t size)

{

for(size_t ix=0; ix!=size;++ix) {

cout
<<arr[ix]<<'';

}

cout
<<endl;

}



//Takes 2 pointers. One to the beginning of the array,

//and one to 1 past the last element - just like iterating through a vector.

voidprint_arr2(constint
beg,constintend)

{

for(/
empty/; beg!=end;++beg) {

cout
<<
beg<<'';

}



cout
<<endl;

}



//Takes an array of size 10 and uses it as a const reference.

voidprint_arr3(constint(&arr)[10])

{

size_t size
=10;

for(size_t ix=0; ix!=size;++ix) {

cout
<<arr[ix]<<'';

}

cout
<<endl;

}



intmain()

{

intarr[]={0,1,2,3,4,5,6,7,8,9};

int*parr=arr;



print_arr1(parr,
10);

print_arr2(parr, parr
+10);

print_arr3(arr);



returnEXIT_SUCCESS;

}
原文链接: https://www.cnblogs.com/yyw84/archive/2011/06/28/2091896.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/27788

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午5:22
下一篇 2023年2月8日 上午5:22

相关推荐