博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c 语言 函数返回数组_如何在C ++函数中返回数组
阅读量:2530 次
发布时间:2019-05-11

本文共 4285 字,大约阅读时间需要 14 分钟。

c 语言 函数返回数组

介绍 (Introduction)

In this tutorial, we are going to understand how we can return 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 .

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

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.

让我们开始吧。

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

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.

让我们看看如何。

#include
using 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.

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

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

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

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

#include 
using 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

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

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()返回的数组是成功的。

结论 (Conclusion)

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.

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

参考资料 (References)

  • – StackOverflow Question,

    – StackOverflow问题,
  • – Journal Dev Post.

    – Journal Dev Post。

翻译自:

c 语言 函数返回数组

转载地址:http://uwlzd.baihongyu.com/

你可能感兴趣的文章
【语言处理与Python】5.3使用Python字典映射词及其属性
查看>>
设备信息
查看>>
Android Volley框架的使用(三)
查看>>
[错误总结] 控件frame设置无效
查看>>
Redis Java API
查看>>
oracle 查询表的定义语句
查看>>
Android 笔记之 Android 系统架构
查看>>
状压dp终极篇(状态转移的思想)
查看>>
AtCoder Grand Contest 031 B - Reversi
查看>>
完整成功配置wamp server小记
查看>>
build.gradle添加Oracle jdbc6 链接
查看>>
影响系统性能的20个瓶颈--转自开源中国
查看>>
根据ISBN获取豆瓣API提供的图书信息
查看>>
【转】Python中*args和**kwargs的区别
查看>>
git命令简单使用
查看>>
CODEFORCES 125E MST Company 巧用Kruskal算法
查看>>
C++标准库分析总结(三)——<迭代器设计原则>
查看>>
Ibatis 配置问题
查看>>
Notability 3.1 for Mac 中文共享版 – 好用的文本笔记工具
查看>>
HDU 2089 数位dp入门
查看>>