返回整数数组从功能不带参数 [英] Returning integer array from function with no arguments

查看:141
本文介绍了返回整数数组从功能不带参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/3127507/returning-local-data-from-functions-in-c-and-c-via-pointer\">Returning通过指针从C和C ++函数的本地数据

Possible Duplicate:
Returning local data from functions in C and C++ via pointer

我需要创建一个函数没有参数,返回一个数组

I need to create a function with no arguments that returns an array

我得到的错误:警告:函数返回局部变量的地址

I get the error: "warning: function returns address of local variable"

我的code已被简化的阅读,便于

my code has been simplified for ease of reading

int * getNums()
{
    int nums[8];
    nums = {1,2,3,4,5,6,7,8};
    return nums;
}

我带领明白,当函数结束指针丢失,但阵列仍发送?如果没有,什么是返回与函数调用没有参数?

I am led understand that when the function ends the pointer is lost, but will the array still be sent? If not, what is a good way to return this integer array with no arguments in the function call?

鸭preciate的帮助提前!

Appreciate the help in advance!

干杯

推荐答案

没有,阵列将不会被派。你需要做的其中之一:

No, the array will not be "sent". You need to do one of these:


  • 动态创建数组使用新的

  • 静态创建数组

  • 数组到函数传递为指针

  • 使用的std ::矢量

在大多数情况下,最后是preferred溶液

In most cases, the last is the preferred solution.

这篇关于返回整数数组从功能不带参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆