#return array

23 messages · Page 1 of 1 (latest)

white axle
#

how would i return an array from a function and use it in another function

char board[6][7]={"","","","","",""}
this is my array

chrome tundraBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

vestal geyser
#

you need to either create it from outside the function and pass it to the function to fill it or allocate it inside your function and return a pointer

white axle
#

so to return a pointer all i have to do is just
return board;

#

?

#

it gives me a warning if i do that

vestal geyser
#

yeah, that won't work

#

you would need a dynamic allocation for that

white axle
#

how would i do that

vestal geyser
#

in C you'd probably use malloc()

white axle
#

and theres no other way?

cursive rain
#

there are, but they're basically just different flavors of malloc

#

or passing in already-allocated memory as already suggested

white axle
cursive rain
#

it's because an array inside your function will disappear when the function ends, so you can't use that to get the memory to return. So the memory needs to come from somewhere else, like malloc or from the caller

white axle
#

i think im gonna go back to python lol

vestal geyser
white axle
#

im supposed to learn c before cpp

#

thas what my teacher told me

vestal geyser
#

alright, good luck!

white axle
#

ty