简体   繁体   中英

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. 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. 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. 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. 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 * 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).

You can rewrite returnIntArray() as following:

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

I did not understand what was the point of "takesTheIntArray" maybe a debug purpose to see x, y, z values in debugger? 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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