Reply to topic  [ 6 posts ] 
C++ array pointer parameters 
Author Message
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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?

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Apr 03, 2012 8:54 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Never mind, just needed to remove the *.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Apr 03, 2012 8:56 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
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

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Tue Apr 03, 2012 9:29 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Apr 03, 2012 10:32 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:58 pm
Posts: 8767
Location: behind the sofa
Reply with quote
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:

_________________
jonbwfc's law: "In any forum thread someone will, no matter what the subject, mention Firefly."

When you're feeling too silly for x404, youRwired.net


Tue Apr 03, 2012 10:53 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
Reply with quote
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.

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Wed Apr 04, 2012 12:25 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 16 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.