数组003 一维数组用于函数的参数

#include<iostream>
using namespace std;

//
//    一维数组用作函数的参数时候(实参),只能传数组的地址,并且一定要把数组的长度传进去,
//        除非数组中有最后一个元素的标志(必须有办法判断地址结尾是哪,要不然容易越界)
//

void pcout(int* arr, int len)
{
    for (int i = 0; i < len; i++)
    {
        cout << *(arr + i) << endl;
    }
}
void pcout1(int arr[], int len)
{
    for (int i = 0; i < len; i++)
    {
        cout << arr[i] << endl;
    }
}

int main(void)
{
    int a[3] = { 1,2,3 };
    pcout1(a, sizeof(a) / sizeof(int));


    return 0;
}

 

posted @ 2024-07-15 16:00  墨点Moz  阅读(18)  评论(0)    收藏  举报