Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to return an array from a sub-routine.

Reply
Thread Tools

How to return an array from a sub-routine.

 
 
Neo
Guest
Posts: n/a
 
      12-01-2003
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.

I am a newbie and have never played with structures or string arrays.

Thanks in advance.

Take care
Rizwan
 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      12-01-2003
On 1 Dec 2003 03:52:40 -0800, in comp.lang.c ,
http://www.velocityreviews.com/forums/(E-Mail Removed) (Neo) wrote:

>Dear All,
>I want to know how a subroutine should return an array of values to
>the main program.


FYI C programmers don't use the word "subroutine", in C its called a
function.

To return an array or struct from a function, pass a pointer to the
object into the function
struct stype s;
char a[12];

void dosomethingtos( &s );
void dosomethingtoa( &a );

You can also di it by using a static or global variable, but this is a
highly dangerous technique and makes your code non-threadsafe and
non-reentrant. For work on modern OSes this is a Bad Idea.


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
 
 
 
Mark A. Odell
Guest
Posts: n/a
 
      12-01-2003
Mark McIntyre <(E-Mail Removed)> wrote in
news(E-Mail Removed):

> On 1 Dec 2003 03:52:40 -0800, in comp.lang.c ,
> (E-Mail Removed) (Neo) wrote:
>
>>Dear All,
>>I want to know how a subroutine should return an array of values to
>>the main program.

>
> FYI C programmers don't use the word "subroutine", in C its called a
> function.
>
> To return an array or struct from a function, pass a pointer to the
> object into the function
> struct stype s;
> char a[12];
>
> void dosomethingtos( &s );
> void dosomethingtoa( &a );
>
> You can also di it by using a static or global variable, but this is a
> highly dangerous technique and makes your code non-threadsafe and
> non-reentrant. For work on modern OSes this is a Bad Idea.


Or just put it in a struct like we've mentioned countless times before.

struct WrapArr
{
int arr[1024];
};

struct WrapArr foo(struct WrapArr s)
{
memset(s.arr, 0, sizeof s.arr);

return s;
}

int main(void)
{
struct WrapArr arr;
struct WrapArr brr;

brr = foo(&arr);

return 0;
}

--
- Mark ->
--
 
Reply With Quote
 
tigervamp
Guest
Posts: n/a
 
      12-01-2003
> 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
 
Reply With Quote
 
nobody
Guest
Posts: n/a
 
      12-02-2003
"Mark A. Odell" <(E-Mail Removed)> wrote in message
news:Xns94445F07310F8CopyrightMarkOdell@130.133.1. 4...
> Mark McIntyre <(E-Mail Removed)> wrote in
> news(E-Mail Removed):
>
> > On 1 Dec 2003 03:52:40 -0800, in comp.lang.c ,
> > (E-Mail Removed) (Neo) wrote:
> >
> >>Dear All,
> >>I want to know how a subroutine should return an array of values to
> >>the main program.

> >
> > FYI C programmers don't use the word "subroutine", in C its called a
> > function.
> >
> > To return an array or struct from a function, pass a pointer to the
> > object into the function
> > struct stype s;
> > char a[12];
> >
> > void dosomethingtos( &s );
> > void dosomethingtoa( &a );
> >
> > You can also di it by using a static or global variable, but this is a
> > highly dangerous technique and makes your code non-threadsafe and
> > non-reentrant. For work on modern OSes this is a Bad Idea.

>
> Or just put it in a struct like we've mentioned countless times before.
>
> struct WrapArr
> {
> int arr[1024];
> };
>
> struct WrapArr foo(struct WrapArr s)
> {
> memset(s.arr, 0, sizeof s.arr);
>
> return s;
> }
>
> int main(void)
> {
> struct WrapArr arr;
> struct WrapArr brr;
>
> brr = foo(&arr);

----------------^---
To be consistent with declaration, it should be
brr = foo(arr);
Though I prefer passing pointer(s) in this case
(as Mark McIntyre suggested), not structs themselves.
>
> return 0;
> }
>
> --
> - Mark ->
> --



 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      12-02-2003
On 1 Dec 2003 03:52:40 -0800, (E-Mail Removed) (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>>
 
Reply With Quote
 
Mark A. Odell
Guest
Posts: n/a
 
      12-02-2003
"nobody" <(E-Mail Removed)> wrote in
news:tETyb.118273$(E-Mail Removed) .cable.rogers.com:


>> int main(void)
>> {
>> struct WrapArr arr;
>> struct WrapArr brr;
>>
>> brr = foo(&arr);

> ----------------^---
> To be consistent with declaration, it should be
> brr = foo(arr);
> Though I prefer passing pointer(s) in this case
> (as Mark McIntyre suggested), not structs themselves.


You are right! Sorry for the bad mistake.

--
- Mark ->
--
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
how do i pass an array to a function .. and also return an array back from the func???? bhejafry.mmk@gmail.com C Programming 3 11-30-2006 05:05 PM
Uniform vector class, inheriting from Array: How to make sure thatmethods return a Vector and not an Array? Thomas Ruby 7 05-23-2005 04:21 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Find if elements of one array are present in the other and return a boolean array Shalini C++ 2 01-09-2004 06:13 PM



Advertisments