Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Draco5869

How do i pass arrays into functions?

This topic is 5495 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Yeah, very ''nubletish'' question, but my 21 day book didn''t explain it cause arrays were way after functions. I have a two dimensional array bUsed[4][4] and i wanna put it into a function PrintValues(). What''s the passing type, what exactly do i pass in, please help. ----------------------------------------------------------- Like a sponge!

Share this post


Link to post
Share on other sites
Advertisement
If you know the size of the array when writing the code you could do:
void func(bool bArray[4][4])
{
...
}

If not, I suggest using pointer to pointer to your datatype, like this:
void func(bool **bArray)
{
...
}


"For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!"

"If anyone sees a suspicious, camouflaged factory being carried across the desert, they should report it immediately."

Share this post


Link to post
Share on other sites
Awesome, got it. My problem was really elseware but i got it all figured out. Thanks.

-----------------------------------------------------------
Like a sponge!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Valderman
If you know the size of the array when writing the code you could do:
void func(bool bArray[4][4])
{
...
}

If not, I suggest using pointer to pointer to your datatype, like this:
void func(bool **bArray)
{
...
}

<hr size=1><font size=1>"For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!"

"If anyone sees a suspicious, camouflaged factory being carried across the desert, they should report it immediately ."</font>



uh don''t do that.

int temp[5][5] is not int**, its an int*.

Share this post


Link to post
Share on other sites
quote:
int temp[5][5] is not int**, its an int*.

Nope.

void func(char **c)
{
printf("%c", c[0][0]);
}

...compiles nicely, while...

void func(char *c)
{
printf("%c", c[0][0]);
}

...gives a "subscript requires array or pointer type".


"For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!"

"If anyone sees a suspicious, camouflaged factory being carried across the desert, they should report it immediately."

Share this post


Link to post
Share on other sites
Julio wrote
"look up passing by reference, to save memory. something like this:
void func(int* &array) "

Assuming we are talking about c++ here (since you used a reference), how does this save memory over

void func(int* array)

or

void func(int array[5])

?

I don''t see that it saves anything?

Share this post


Link to post
Share on other sites
i dont know if you can...well...u can...but i dont think that that particulay function is gonna cut it....you can always loop it

take my include files for granted

int main()

{

int x=1, y=1;

while (x<=4||y<=4)

{

cout<
x++;

if (x==4)

{

x=0;

y++;

}

}

return 0;

}

didnt check it at all.....but yah...it might help a bit

Share this post


Link to post
Share on other sites
Put that in [ source ] tags, it's pretty unreadable in its current state.

"For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!"

"If anyone sees a suspicious, camouflaged factory being carried across the desert, they should report it immediately ."


[edited by - Valderman on July 6, 2003 10:22:57 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Julio
look up passing by reference, to save memory. something like this:
void func(int* &array)




Either one passes a 32-bit pointer.. i don''t see how you''re saving anything...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!