返回数组中的函数 [英] Return array in a function
问题描述
我有一个数组 INT ARR [5]
传递给函数 fillarr(INT ARR [])
:
INT fillarr(INT ARR [])
{
对于(...);
返回ARR;
}
- 如何返回数组?
- 我将如何使用它,说我回来的指针我应该如何去访问它?
在这种情况下,您的数组变量改编
实际上也被视为指针的开始您的数组的内存块,通过隐式转换。此语法您使用的是:
INT fillarr(INT ARR [])
时的那种只是语法糖。你真的可以用这个代替它,它仍然工作:
INT fillarr为(int * ARR)
因此,在同样的意义,你想从你的函数返回的内容实际上是一个指向数组的第一个元素:
为int * fillarr(INT ARR [])
之后,你仍然可以使用它,就像你将一个正常的数组:
INT的main()
{
INT Y [10];
为int * A = fillarr(Y);
COUT<<一个[0]&下;&下; ENDL;
}
I have an array int arr[5]
that is passed to a function fillarr(int arr[])
:
int fillarr(int arr[])
{
for(...);
return arr;
}
- How can I return that array?
- How will I use it, say I returned a pointer how am I going to access it?
In this case, your array variable arr
can actually also be treated as a pointer to the beginning of your array's block in memory, by an implicit conversion. This syntax that you're using:
int fillarr(int arr[])
Is kind of just syntactic sugar. You could really replace it with this and it would still work:
int fillarr(int* arr)
So in the same sense, what you want to return from your function is actually a pointer to the first element in the array:
int* fillarr(int arr[])
And you'll still be able to use it just like you would a normal array:
int main()
{
int y[10];
int *a = fillarr(y);
cout << a[0] << endl;
}
这篇关于返回数组中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!