数组指针与地址
学习C++写了下面一段代码, 然后对其中一个小小问题(这问题很low,看到的兄弟别笑话我哈哈)思考了一下:
下面这个函数,为什么原型是int arr[ ] ,调用时候用的却是数组的名称呢?
原来我一直混淆了指针和地址的概念,地址就像是你家的位置,而指针像是指向你家的一个箭头;
所以 int arr[ ] 是一个数组的地址;a 是数组的第一个地址;对应上了。
还有例如 int * a ;a表示的是指针,而不是 * a。感觉还是看书不细致导致的。继续加油吧。
int fill_arrat(int arr[], int n);
‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘
int sum = fill_arrat(a, limit);
#include<iostream>
using namespace std;
int fill_arrat(int arr[], int n);
const int limit = 6;
int main()
{
int a[limit];
int sum = fill_arrat(a, limit);
cout << sum <<endl;
return 0;
}
int fill_arrat(int arr[], int n)
{
int i;
int temp;
for ( i = 0; i < limit; i++)
{
cin >> temp;
if (!cin)
{
cin.clear();
while (cin.get() != '\n')
continue;
}
else if (temp < 0)
break;
arr[i] = temp;
}
return i;
}