如有错误,还请指正!
目前学C++学到将数组形参传递到函数中,对数组进行操作。
在编程时,由于传入指针是数组的头指针,函数并不知道数组的大小,我就差了一下资料,方法可能不全。
第一种方法,将数组的开始地址与结束地址作为形参,用了两种方法遍历:递归和while循环。
void OutPut(const int* array_begin, const int* array_end) { if (array_begin == array_end) { return; } cout << *array_begin << endl; OutPut(array_begin + 1, array_end); //while(*array_begin != *array_end) //{ // cout << *array_begin << endl; // array_begin++; //} }
第二种方法,将确定的数组大小传进来,这种方式限制了数组的大小。
void OutPut(int (&a)[3]) { for (auto& p : a) { cout << p << endl; } }
第三种方法,将数组和数组大小当做形参。
void OutPut(int a[], size_t array_size) { for (size_t i = 0; i != array_size; i++) { cout << a[i] << endl; } }
原文链接: https://www.cnblogs.com/gk520/p/15471251.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/404314
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!