On 1 Dec 2003 03:52:40 -0800,
(Neo) wrote:
>Dear All,
>I want to know how a subroutine should return an array of values to
>the main program.
>From the main program, I call a sub-routine 'get_sql' which then
>fetches data from oracle db using oci8 routines. The output resides in
>a structure defined within the sub-routine. Now I want this structure
>to be returned to main program so that I can assing output data to
>variables in main program and do some manipulation. Can any body guide
>me about how to do it.
>
Make up your mind. Do you want to return an array or a structure?
Returning a structure makes for simpler code (but not necessarily
efficient code). You return the structure with a return statement
like
return name_of_my_struct;
This will work even if the structure contains one or more arrays.
Returning an independent array can be more complicated. Among the
options are
Define the array in the function as static. This will insure the
contents of the array survive past the end of the function.
Allocate the array using malloc or one of its cousins. You can
then return the address of the array to the calling function which
will be responsible to free() the allocated memory when it is no
longer needed.
Define the array in the calling function and pass it to the called
function. The called function will then be able to update the array
directly and it does not need to be returned explicitly.
Define the array at file scope (a global array). The called
function will then be able to update the array directly and it does
not need to be returned explicitly.
<<Remove the del for email>>