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

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 pointers.

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

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<iostream>
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<<ptr[i]<<"\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 structure in C++. Let us see how.

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


#include <iostream>
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<n;i++)
	{
		//array initialisation
		demo_mem.arr[i] = i;
	}
	return demo_mem; //address of structure member returned
}

int main() 
{
	struct demo a;
	int n=5; //number of elements
	
	a=func(n); //address of arr
	
	cout<<"The Array is : ";
	for(int i=0;i<n;i++)
	{
		cout<<a.arr[i]<<"\t";
	}
	
	return 0;
}

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 <iostream>
#include<array>
using namespace std;

std::array<int,5> func() //function with return type std::array
{
    std::array<int,5> 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<int,5> arr; //array with length 5
	
	arr=func(); //function call
	
	cout<<"The Array is : ";
	for(int i=0;i<5;i++)
	{
		cout<<arr[i]<<"\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)

翻译自: https://www.journaldev.com/39081/return-array-in-c-plus-plus-function

c 语言 函数返回数组

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值