x404.co.uk
http://www.x404.co.uk/forum/

C++ array pointer parameters
http://www.x404.co.uk/forum/viewtopic.php?f=4&t=16201
Page 1 of 1

Author:  Fogmeister [ Tue Apr 03, 2012 8:54 pm ]
Post subject:  C++ array pointer parameters

I'm trying to pass an array pointer into a function to manipulate it.

I can't work out the syntax for the parameters and how to pass it in.

Something like...

Code:
void manipulateArray(int* array[9])
{
    array[3] = 0;
}

int main(int argc, const char * argv[])
{
    int array[9];

    for (int i = 0; i < 9; i++)
    {
        array[i] = i;
    }
   
    manipulateArray(array);
   
    for (int i = 0; i < 9; i++)
    {
        cout << array[i] << endl;
    }
   
    return 0;
}


But I can't work out the syntax.

Any help?

Author:  Fogmeister [ Tue Apr 03, 2012 8:56 pm ]
Post subject:  Re: C++ array pointer parameters

Never mind, just needed to remove the *.

Author:  finlay666 [ Tue Apr 03, 2012 9:29 pm ]
Post subject:  Re: C++ array pointer parameters

Fogmeister wrote:
Never mind, just needed to remove the *.

Or IIRC (my C++ is a bit rusty) use '&' to get the physical instead of the pointer

Author:  Fogmeister [ Tue Apr 03, 2012 10:32 pm ]
Post subject:  Re: C++ array pointer parameters

finlay666 wrote:
Fogmeister wrote:
Never mind, just needed to remove the *.

Or IIRC (my C++ is a bit rusty) use '&' to get the physical instead of the pointer

I was trying to pass the pointer so that the function I called was manipulating the actual array rather than a copy of it.

I then found a reference on the t'interwebs that said something along the lines of "C++ arrays are already pointers". i.e. by passion gthe array you are actually just passing a pointer to the array.

Author:  JJW009 [ Tue Apr 03, 2012 10:53 pm ]
Post subject:  Re: C++ array pointer parameters

Fogmeister wrote:
C++ arrays are already pointers

Forgetting this was a classic C mistake which used to commonly lead to very horrible problems, like randomly writing over parts of memory being used by the OS :lol:

Author:  EddArmitage [ Wed Apr 04, 2012 12:25 pm ]
Post subject:  Re: C++ array pointer parameters

Fogmeister wrote:
"C++ arrays are already pointers".

Nearly, and it's something you'll hear a lot of the time. For some differences, see here; for some freakiness, see here.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/