700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > c 语言 函数返回数组_如何在C ++函数中返回数组

c 语言 函数返回数组_如何在C ++函数中返回数组

时间:2023-02-23 02:58:26

相关推荐

c 语言 函数返回数组_如何在C ++函数中返回数组

c 语言 函数返回数组

介绍 (Introduction)

In this tutorial, we are going to understand how we canreturn an array from a function in C++.

在本教程中,我们将了解如何从C ++中的函数返回数组

在C ++函数中返回数组的方法 (Methods to Return an Array in a C++ Function)

Typically, returning a whole array to a function call is not possible. We could only do it using pointers.

通常,无法将整个数组返回给函数调用。 我们只能使用指针来做。

Moreover, declaring a function with a return type of a pointer and returning the address of aCtype 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 inC++is not that easy. But we can accomplish that by following any of the below mentioned methods.

因此,从C ++中的函数返回数组并不是那么容易。 但是我们可以通过遵循以下任何一种方法来实现。

Let’s get right into it.

让我们开始吧。

1.使用指针 (1. Using Pointers)

As we mentioned earlier, returning a normalarrayfrom a function usingpointerssometimes gives us unexpected results. But this behaviour and warnings can be avoided by declaring the array to be astaticone.

如前所述,使用指针从函数返回普通数组有时会给我们带来意想不到的结果。 但是可以通过将数组声明为static数组来避免这种行为和警告。

Let us see how.

让我们看看如何。

#include<iostream>using namespace std;int* demo() //return type- address of integer array{static int a[5]; //array declared as staticfor(int i = 0; i<5; i++){a[i] = i; //array initialisation}return a; //address of a returned}int main(){int* ptr; //pointer to hold addressint i;ptr = demo(); //address of acout<<"Array is: ";for(i=0 ; i<5; i++)cout<<ptr[i]<<"\t"; //ptr[i] is equivalent to *(ptr+i)return 0;}

Output:

输出:

Array is: 01 2 3 4

Here, we have declared the functiondemo()with a return typeint *(pointer) and in its definition, we have returneda(serves as both array name and base address) to site of the function call inmain().

在这里,我们用返回类型int *(指针)声明了函数demo(),并在其定义中向main()中的函数调用位置返回a(同时用作数组名和基地址main()

As we can see from the above output, the array is successfullyreturnedby the function.

从上面的输出中可以看到,该函数成功返回了数组。

2.在C ++中使用结构 (2. Using a Structure in C++)

We can also make a function return an array by declaring it inside a structure in C++. Let us see how.

我们还可以通过在C ++中的结构内部声明函数来使函数返回数组。 让我们看看如何。

#include <iostream>using namespace std;struct demo{//array declared inside structureint arr[100];};struct demo func(int n) //return type is struct demo{struct demo demo_mem; //demo structure member declaredfor(int i=0;i<n;i++){//array initialisationdemo_mem.arr[i] = i;}return demo_mem; //address of structure member returned}int main() {struct demo a;int n=5; //number of elementsa=func(n); //address of arrcout<<"The Array is : ";for(int i=0;i<n;i++){cout<<a.arr[i]<<"\t";}return 0;}

Output:

输出:

Array is: 01 2 3 4

Here, note that we have declared the arrayarrinside thestructuredemo. And this time the function has a return type of the structure itself and returndemo_mem(structure variable) instead of the array.

在这里,请注意,我们已经在结构demo声明了数组arr。 这次函数具有结构本身的返回类型,并返回demo_mem(结构变量)而不是数组。

In this way using another structure variablea, we can access the arrayarrin themain()function.

这样,使用另一个结构变量a,我们可以在main()函数中访问数组arr

3.使用std :: array (3. Using std::array)

Forstd::arrayinC++, 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 <iostream>#include<array>using namespace std;std::array<int,5> func() //function with return type std::array{std::array<int,5> f_array; //array declaredfor(int i=0;i<5;i++){//array initialisationf_array[i] = i;}return f_array; //array returned}int main() {std::array<int,5> arr; //array with length 5arr=func(); //function callcout<<"The Array is : ";for(int i=0;i<5;i++){cout<<arr[i]<<"\t";}return 0;}

Output:

输出:

Array is: 01 2 3 4

Hence it is clear from the output, that the array return by the functionfunc()was successful.

因此,从输出中可以明显看出,函数func()返回的数组是成功的。

结论 (Conclusion)

So in this tutorial, we learned about the different methods by which we canreturn an array from a C++ function.

因此,在本教程中,我们了解了可以从C ++函数返回数组的不同方法。

For any further questions, feel free to use the comments below.

如有其他疑问,请随时使用以下评论。

参考资料 (References)

C++ return array from function – StackOverflow Question,函数的C ++返回数组 – StackOverflow问题, Two Dimensional Array in C++ – Journal Dev Post.C ++中的二维数组 – Journal Dev Post。

翻译自: /39081/return-array-in-c-plus-plus-function

c 语言 函数返回数组

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。