On Nov 8, 2007 2:35 AM, Wangyuan <[EMAIL PROTECTED]> wrote:

> Yestoday I use a function to make an array . now I want to return the array 
> to main() . but I can't return the array in the whole , that is , I can only 
> return it's
>  elements one by one , using for{ }. Can you give me an easy and brief  way
>  to return array .
>  THANKS VERY MUCH !

You can't return an array directly from a function, but you CAN return
a pointer to the start of the array -- but be careful, you can't use a
pointer to an array that is only local to the function. You will need
to either pass the  pre-allocated array into the function or
dynamically allocate memory for the array inside the function and then
return a pointer to that allocated memory. A lot of the string
functions in C do this.

Another option is to wrap the array in a struct and return that (or a
pointer to the struct) ... but even better is to use C++ and use one
of the high-level container classes (like vector).

-- Brett
------------------------------------------------------------
"In the rhythm of music a secret is hidden;
    If I were to divulge it, it would overturn the world."
               -- Jelaleddin Rumi

Reply via email to