C++ 函数传参

C++和C语言一样,也将数组视为指针。

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include"stdafx.h"
#include<iostream>
#include<vector>



void count(int a[10]){
	for (int i = 0; i < 10; i++){
		a[i] = i;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	using namespace std;
	int a[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };

	count(a);

	cout << sizeof(a) << endl;

	for (int i = 0; i < sizeof(a)/sizeof(int); i++){
		cout << a[i] << endl;
	}
	

	//program ending;
	cin.get();
	return 0;
}



注意:
数组已初始化的情况下,后面代表的是下标,如a[10]是指a的第10个元素
传递数组时,函数使用该变量的拷贝,但是使用数组时,函数将使用原来的数组。
一般不许修改记得加 const常量修饰。

原文地址:https://www.cnblogs.com/canbefree/p/5207092.html