本文共 4285 字,大约阅读时间需要 14 分钟。
c 语言 函数返回数组
In this tutorial, we are going to understand how we can return an array from a function in C++.
在本教程中,我们将了解如何从C ++中的函数返回数组 。
Typically, returning a whole array to a function call is not possible. We could only do it using .
通常,无法将整个数组返回给函数调用。 我们只能使用来做。
Moreover, declaring a function with a return type of a pointer and returning the address of a C type array in C++ doesn’t work for all cases. The compiler raises a warning for returning a local variable and even shows some abnormal behavior in the output.
此外,在C ++中用指针的返回类型声明函数并返回C类型数组的地址并不适用于所有情况。 编译器会针对返回局部变量发出警告,甚至在输出中显示一些异常行为。
Hence, returning an array from a function in C++ is not that easy. But we can accomplish that by following any of the below mentioned methods.
因此,从C ++中的函数返回数组并不是那么容易。 但是我们可以通过遵循以下任何一种方法来实现。
Let’s get right into it.
让我们开始吧。
As we mentioned earlier, returning a normal array from a function using pointers sometimes gives us unexpected results. But this behaviour and warnings can be avoided by declaring the array to be a static
one.
如前所述,使用指针从函数返回普通数组有时会给我们带来意想不到的结果。 但是可以通过将数组声明为static
数组来避免这种行为和警告。
Let us see how.
让我们看看如何。
#includeusing namespace std;int* demo() //return type- address of integer array{ static int a[5]; //array declared as static for(int i = 0; i<5; i++) { a[i] = i; //array initialisation } return a; //address of a returned}int main(){ int* ptr; //pointer to hold address int i; ptr = demo(); //address of a cout<<"Array is: "; for(i=0 ; i<5; i++) cout< <<"\t"; //ptr[i] is equivalent to *(ptr+i) return 0;}
Output:
输出:
Array is: 0 1 2 3 4
Here, we have declared the function demo()
with a return type int *
(pointer) and in its definition, we have returned a
(serves as both array name and base address) to site of the function call in main()
.
在这里,我们用返回类型int *
(指针)声明了函数demo()
,并在其定义中向main()
中的函数调用位置返回a
(同时用作数组名和基地址main()
。
As we can see from the above output, the array is successfully returned by the function.
从上面的输出中可以看到,该函数成功返回了数组。
We can also make a function return an array by declaring it inside a . Let us see how.
我们还可以通过的内部声明函数来使函数返回数组。 让我们看看如何。
#includeusing namespace std;struct demo{ //array declared inside structure int arr[100];};struct demo func(int n) //return type is struct demo{ struct demo demo_mem; //demo structure member declared for(int i=0;i
Output:
输出:
Array is: 0 1 2 3 4
Here, note that we have declared the array arr
inside the structure demo
. And this time the function has a return type of the structure itself and return demo_mem
(structure variable) instead of the array.
在这里,请注意,我们已经在结构 demo
声明了数组arr
。 这次函数具有结构本身的返回类型,并返回demo_mem
(结构变量)而不是数组。
In this way using another structure variable a
, we can access the array arr
in the main()
function.
这样,使用另一个结构变量a
,我们可以在main()
函数中访问数组arr
。
For std::array
in C++, returning the array name from a function actually translates into the the whole array being returned to the site of the function call.
对于C ++中的 std::array
,从函数返回数组名称实际上转化为要返回到函数调用站点的整个数组。
#include#include using namespace std;std::array func() //function with return type std::array{ std::array f_array; //array declared for(int i=0;i<5;i++) { //array initialisation f_array[i] = i; } return f_array; //array returned}int main() { std::array arr; //array with length 5 arr=func(); //function call cout<<"The Array is : "; for(int i=0;i<5;i++) { cout< <<"\t"; } return 0;}
Output:
输出:
Array is: 0 1 2 3 4
Hence it is clear from the output, that the array return by the function func()
was successful.
因此,从输出中可以明显看出,函数func()
返回的数组是成功的。
So in this tutorial, we learned about the different methods by which we can return an array from a C++ function.
因此,在本教程中,我们了解了可以从C ++函数返回数组的不同方法。
For any further questions, feel free to use the comments below.
如有其他疑问,请随时使用以下评论。
翻译自:
c 语言 函数返回数组
转载地址:http://uwlzd.baihongyu.com/