简体   繁体   English

c ++将指针传递给函数,用于将int数组传递出函数

[英]c++ passing Pointers into a function, for passing int arrays out of and into a function

I am trying to pass an int array around. 我试图传递一个int数组。 Below is an example of what I want to do. 以下是我想要做的一个例子。 Basically, I can write a function that returns an int array by returning a pointer. 基本上,我可以通过返回指针来编写一个返回int数组的函数。 Now I want to take that function and use it as an argument to another function. 现在我想要使用该函数并将其用作另一个函数的参数。 The goal is to have one function create an int array and then this goes into another function that takes an int array as an input. 目标是让一个函数创建一个int数组,然后进入另一个以int数组作为输入的函数。 It doesn't work. 它不起作用。 Inside the function that takes the int * pointer, the int * pointer just becomes -8435432 and can't have its elements read after it is assigned to another int * pointer. 在获取int *指针的函数内部,int *指针变为-8435432,并且在将其分配给另一个int *指针后不能读取其元素。 I don't get it. 我不明白。 Why can I get an int array back from a function but this can't then be used as an input to another function? 为什么我可以从函数中获取一个int数组,但是这不能用作另一个函数的输入?

int * returnIntArray()
{
    int * thePointer;
    int j[3];
    thePointer = j;

    j[0] = 1;
    j[1] = 3;
    j[2] = -1;
    return thePointer;
}

//
int * takesTheIntArray(int * anIntArray)
{
    int x,y,z;
    int * returnIt;
    returnIt = anIntArray;
    x = returnIt[0];
    y = returnIt[1];
    z = returnIt[3];
    return returnIt;
}


int _tmain(int argc, _TCHAR* argv[])
{
    int y,z,p;
    int * x;
    x = returnIntArray();
    y = x[0];
    z = x[1];
    x = takesTheIntArray(returnIntArray());
    cout << x[0] << ",  " << x[1];
    //cout << theVector[1];
    cout << "hello";
}
int * thePointer;
    int j[3];
    thePointer = j;

Should be: 应该:

int* thePointer = new int[3];

The problem with the first version is that you're returning something that is statically allocated (allocated on the stack). 第一个版本的问题是你正在返回静态分配的东西(在堆栈上分配)。 It will go out of scope when the function returns. 函数返回时,它将超出范围。 In the second version it's dynamically allocated (allocated on the heap). 在第二个版本中,它是动态分配的(在堆上分配)。 Remember to call delete[] on thePointer once you're finished (or else you leak memory). 记得在完成后调用thePointer上的delete[] (否则你会泄漏内存)。

You can rewrite returnIntArray() as following: 您可以重写returnIntArray(),如下所示:

int * returnIntArray()
{
    static int j[3];

    j[0] = 1;
    j[1] = 3;
    j[2] = -1;
    return j;
}

You don't need the "thePointer" variable that only hides the fact that your j was a local variable destroyed at the end of the scope. 您不需要“thePointer”变量,它只隐藏您的j是在作用域末尾销毁的局部变量这一事实。 You may have tried without the "thePointer" variable and obtained a "warning C4172: returning address of local variable or temporary" meaning the value you return won't be valid after returning (the same problem you have with thePointer). 您可能尝试过没有“thePointer”变量并获得“警告C4172:返回本地变量或临时地址”,这意味着您返回的值在返回后将无效(与您使用的相同问题)。 The static before j declaration makes the j global (but only available by its name in the function) so values contained by j won't be lost at the end of the scope. j之前的静态声明使j全局(但只能通过函数中的名称使用),因此j包含的值不会在范围的末尾丢失。

I did not understand what was the point of "takesTheIntArray" maybe a debug purpose to see x, y, z values in debugger? 我不明白“takeTheIntArray”可能是一个调试目的,看看调试器中的x,y,z值是什么意思? if this is the case you don't need to copy the pointer and can simply do: 如果是这种情况,您不需要复制指针,只需执行以下操作:

//
int * takesTheIntArray(int * anIntArray)
{
    int x,y,z;
    x = anIntArray[0];
    y = anIntArray[1];
    z = anIntArray[2];
    return anIntArray;
}

Keep your main and this should work as you want :) [edit] there was a typo in the "takesTheIntArray" I didn't noticed when reposting: the anIntArray index for z var should be 2 and not 3[/edit] 保持你的主要,这应该按你想要的方式工作:) [编辑]“takeTheIntArray”中有一个拼写错误我在重新发布时没有注意到:z var的anIntArray索引应该是2而不是3 [/ edit]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM