Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

[C++] Allocating local variables

Name: Anonymous 2012-09-02 0:12

Shouldn't this be working? I'm trying to point all three pointers to this new int array, but the address isn't right.

<code>void allocate(int a[], int * &x, int ** &y, int *** &z)
{
    a = new int [ 5 ];

    x = a;
    y = new int*;
    *y = a;
    z = new int**;
    *z = new int*;
    **z = a;

    return;
}

int main()
{
    int arr[5];

    int *ptrX;
    int **ptrY;
    int ***ptrZ;

    // Allocate the dynamic memory
    allocate(arr, ptrX, ptrY, ptrZ);

    return 0;
}</code>

Name: Anonymous 2012-09-02 14:18

>>21
"The analogy is just as terrible as ``An array is really just a pointer''."

An array name IS a pointer. Literally. It's a pointer to the first element of the array. A house isn't a vehicle. Your analogy was dumb and proved nothing.

>>23
The code I wrote isn't a complete program, you idiot. Obviously you would deallocate, and add your includes. >>14 is fine, except it doesn't do at all what the OP wanted. Multiple levels of indirection, a dynamic array on the heap and an actual local pointer to that array in main. >>15 does.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List