将数组传递给函数并返回指针

时间:2018-04-13 02:10:05

标签: c++ arrays pointers

对于我的小型作业,在一个30层的建筑物中,我必须收集人们按下电梯的楼层,然后找出每层楼之间的差异。

所以,我打算设置一个30层楼的阵列(我们只教过阵列作为我们唯一的容器)。电梯里的人会点击电梯的按钮,所以假设(5,10,14,19,29)。

然后我计划将这个数组传递给一个函数,该函数将计算每个楼层之间的差异。

到目前为止,这是我的代码,我知道它的错误,因为它没有编译,我也可能在别处错了。

以下是错误消息:

main.cpp: In function 'int* calculateDiff(int*, int)':
main.cpp:26:7: warning: address of local variable 'floorsDiffResult' returned [-Wreturn-local-addr]

CODE

#include <iostream>
#include <numeric>
#include <algorithm>
using std::cout; 
using std::endl;

int* calculateDiff(int floors[], int floorsSize);

int main() 
{
  int floorsPressed[30] = {5, 10, 14, 19, 29};
  int floorsCounter = 5;

  int* ptr = calculateDiff (floorsPressed, floorsCounter);

  int floorsDiffResult[30];
  for (int i = 0; i < floorsCounter; i++)
  {
    floorsDiffResult[i] = *(ptr + i); //Storing the difference into floorsDiffResult array
    cout << "Difference: " << *(ptr + i) << endl;
  }
}

int* calculateDiff(int floors[], int floorsSize)
{
  int floorsDiffResult[30]; //Create another array to store the difference for other calculations later on such as finding the biggest difference, average of the difference etc.
  std::adjacent_difference(floors, floors + floorsSize, floorsDiffResult);
  std::move(floors + 1, floors + floorsSize, floorsDiffResult); //First element does not give the difference

  return floorsDiffResult;
}

2 个答案:

答案 0 :(得分:1)

我不知道你在这里尝试做的事情背后的逻辑是否正确,但这里存在一个主要问题,你正在返回指向本地变量的指针!

这是未定义的行为,因为它是本地的,它的生命周期受限于你的函数范围,之后可能发生任何事情,甚至是你期望的事情(正确的结果)。

所以这就是你可以做的事情:

int* calculateDiff(int floors[], int* output, int floorsSize);

int main()
{
    int floorsPressed[30] = {5, 10, 14, 19, 29};
    int floorsReturn[30] = {};
    int floorsCounter = 5;

    int* ptr = calculateDiff(floorsPressed, floorsReturn, floorsCounter);

    int floorsDiffResult[30];
    for(int i = 0; i < floorsCounter; i++)
    {
        floorsDiffResult[i] = *(ptr + i); //Storing the difference into floorsDiffResult array
        cout << "Difference: " << *(ptr + i) << endl;
    }
}

int* calculateDiff(int floors[], int* output, int floorsSize)
{
    //int floorsDiffResult[30]; //Create another array to store the difference for other calculations later on such as finding the biggest difference, average of the difference etc.
    std::adjacent_difference(floors, floors + floorsSize, output);
    std::move(floors + 1, floors + floorsSize, output); //First element does not give the difference

    return output;
}

并且您不需要从calculateDiff返回指针,floorsReturn会在执行函数后得到您的结果,但我不想更改您的方法。

答案 1 :(得分:0)

该函数的范围将删除其中创建的任何指针的内容。我建议你将输出作为函数中的第三个参数传递:

void calculateDiff(int floors[], int floorsSize, int floorDiffResult [])
{
  //int floorsDiffResult[30]; //Create another array to store the difference for other calculations later on such as finding the biggest difference, average of the difference etc.
  std::adjacent_difference(floors, floors + floorsSize, floorsDiffResult);
  std::move(floors + 1, floors + floorsSize, floorsDiffResult); //First element does not give the difference
}

并以这种方式称呼它:

  int floorsPressed[30] = {5, 10, 14, 19, 29};
  int floorsCounter = 5;
  int floorsDiffResult[30];

  calculateDiff (floorsPressed, floorsCounter, floorsDiffResult);


  for (int i = 0; i < 30; i++)
     cout << "Difference: " << floorsDiffResult[i] << endl;

请注意,在您的代码中,您循环使用floorsCounter(size = 5)来填充floorsDiffResult(size = 30):

for(int i = 0; i < floorsCounter; i++)
    {
        floorsDiffResult[i] = *(ptr + i); //Storing the difference into floorsDiffResult array
        cout << "Difference: " << *(ptr + i) << endl;
    }

确保您没有逻辑错误。