Archived

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

How do i pass arrays into functions?

This topic is 5277 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
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
The name of the array is a pointer to the first element such as bUsed[0][0].

#define LEN 20

so

int array[LEN];

is the same as

int * pt = array[0];

So therefore just pass the array name.

Share this post


Link to post
Share on other sites
i decided to re-write my "mini prog" as a function..since its 1130 at night and i got nothin better to do (gf out of town )

once again....includes are a waste of my time

void arrayprint(int x1, int x2, int y1, int y2 int array)

{

x1=0;
//are those two neccessary?? can i put them in the parameters??
y1=0;

while (x1
{

cout<
x1++;

if (x1==x2)

{

x1=0;

y1++;

}

}

}




that one should be better...can someone tell me if i screwed up??? cause the noob here is lazy...and u guys debugging makes a lot more sense than msvs...yup..thanx

Share this post


Link to post
Share on other sites
Valderman - Your code only works because the size of an int is usually of the same size as a pointer. Multidimensional arrays really are ''flat'' arrays, not really arrays of arrays.

Assuming sizeof(char*) == 4, you would get the following results.

With char array[6][7];, array[2][3] is at offset 2*7+3 = 17.
With char** array;, array[2][3] is at offset 2*4+3 = 11.

The proper way to pass an array is either type*, which forces you to do the offset computation yourself, or type[size][size] which lets the compiler do it by itself. You can omit the leftmost ''size'' parameter (type[][size]) as it doesn''t influence the offset computation and C doesn''t do bounds-checking.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
Sorry then, I didn''t know that.


"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