how do i pass an array to a function .. and also return an array back from the func????

B

bhejafry.mmk

hey ..
i've been tryin this for a long time now ..
jow the bloody hell do i pass an array to the function ..
also .. how do i get the function to return an array ...
all in C language ...
please help
 
C

Chris Dollin

hey ..
i've been tryin this for a long time now ..

How long? Minutes? Hours? Days? Weeks?

Longer than it would take to find a decent book on C and
read it? (I think K&R2 is pretty good, myself.)
jow the bloody hell do i pass an array to the function ..

That depends what you mean by that, but one interpretation
allows you to just write the array name in the argument
position. Did you try that? What happened? Why didn't you
post your trial code -- it's much easier to correct
something than to offer advice in a vacuum.
also .. how do i get the function to return an array ...

Strictly, you can't. But you can return a /pointer/, usually
to the first element of the array. You also need a way of
saying how big the array thingy is. There are other tricks.
Which ones are best depends rather on exactly what you're
trying to do.
 
K

Keith Thompson

i've been tryin this for a long time now ..
jow the bloody hell do i pass an array to the function ..
also .. how do i get the function to return an array ...
all in C language ...

Reading the comp.lang.c FAQ would be a good start.
<http://www.c-faq.com>
 
F

Frederick Gotham

:
hey ..
i've been tryin this for a long time now ..
jow the bloody hell do i pass an array to the function ..
also .. how do i get the function to return an array ...
all in C language ...
please help


Array of any length? You'd need pointers:

int *Func(int *p)
{
p[0] = 6;
p[1] = 7;
p[2] = 2;

return p;
}

Array of constant length? You can use the method above, or:

int (*Func(int (*const parr)[len]))[len]
{
int *p = *parr;

p[0] = 6;
p[1] = 7;
p[2] = 2;

return parr;
}

or, if you'd like to wrap it in a struct:

struct ArrFiveInts {
int arr[5];
};

ArrFiveInts *Func(ArrFiveInts *p);

If you pass an ArrFiveInts by value, then you'll have a copy of the array
in the called function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top