Re: How to return an array from a sub-routine.

From: tigervamp (rob_gamble99_at_hotmail.com)
Date: 12/01/03


Date: 1 Dec 2003 11:28:05 -0800


> 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.

What you want to do is have your subroutine return a pointer to an
array/structure. I don't know enough about your needs, but you may
want to consider creating an array of pointers to values or array of
pointers to structures if the size of the resulting value lengths from
your queries is not always the same (if you are pulling var char
fields for instance).

Here is a basic outline of the idea:

(Assumes function returns a pointer to a structure populated by
get_sql)

struct sql_result { int val1; char * val2; ...};

struct sql_result * sql_get ( your arguments);

int main (void) {
    struct sql_result * sql_res_ptr; /* declare pointer to structure
*/
    ...
    sql_res_ptr = sql_get ( your args);
    value1 = sql_res_ptr->val1; /* Retrieve values, value 1 is int in
this example */
    value2 = sql_res_ptr->val2; /* value2 is type char * here */
    ...
}

struct sql_result * sql_get ( your args ) {
    ...
    static struct sql_result sql_res; /* Not thread safe */
    /* Do struct populating here */
    sql_res.val1 = query_db_for_val1(blah);
    sql_res.val2 = query_db_for_val2(blah);
    return (&sql_res);
    ...
}

The basic idea is to have your sql_get function set aside storage for
your information, populate with your query results and pass back a
pointer to the information. Note that in this example, I used a
static structure so the information your pointer points to will change
the next time this function is called. You could also have the
sql_function use malloc to dynamically allocate memory and then make
it the responsibility of the caller to free the memory when no longer
needed, or you could make the caller allocate the memory and pass the
location of the allocated memory to the function for it to use. If
you are dealing with variable length data, it might make the most
sense to have the function allocate the memory and the caller free it.

Hope this helps,

Rob Gamble



Relevant Pages

  • Re: The value of a CS education
    ... multidimensional arrays badly as a pointer to a pointer. ... demand paged virtual memory machines to their knees. ... Fast algorithms to do this array transpose efficiently have to be cache ... Its interesting to watch how the performance of many applications ...
    (sci.electronics.design)
  • Re: HardBound and SoftBound (was "The State of Software")
    ... completely re-widen, to all of memory, and hence lose all protection. ... e.g. unless you know that malloc'ing is being done out of a common array ... I agree with Nick - SoftBound will fail to detect many common ... interpreted as a pointer across different architectures and memory models... ...
    (comp.arch)
  • Re: Problem with large arrays
    ... >am trying to using an array of signals that is just slightly larger) for the ... location of this very large memory. ... so that each pointer points to one row. ... row data structure and make the pointer point to it. ...
    (comp.lang.vhdl)
  • Re: gdb not catching out-of-bounds pointer
    ... that, for example, accesses one array from a pointer to another is ... provided the library writer knows what the compiler writer guarantees ... The portability of an allocator depends on the source of raw memory. ...
    (comp.unix.programmer)
  • Re: passing a multidimensional array to a function
    ... const int N_HIGHSCAN = 2501; ... allocate memory for iScan_MZ_shift and fill it with 100s ... Still a memory leak. ... Some do advocate assigning NULL to any pointer that is ...
    (comp.lang.c.moderated)