Sign in to follow this  
floatingwoods

Returning various arrays of data. What's the elegant way?

Recommended Posts

floatingwoods    364
Hi, Returning a simple array of data is usually handled by directly returning the data as the return value. But what if I want to return several arrays? I need a pure C solution, so references can't be used. The only way I have come up with is following, but it doesn't seem elegant to me:
void returnSomeArrays(float** array1,int* array1Length, int** array2,int* array2Length)
{
    if (array1Length[0]!=-1)
    {
        array1[0]=new float[23234];
        array1Length[0]=23234;
    }
    if (array2Length[0]!=-1)
    {
        array2[0]=new int[99];
        array2Length[0]=99;
    }
}

and then I would use the function as following:
float* array1;
int* array2;
int array1Length=0;
int array2Length=0;
returnSomeArrays(&array1,&array1Length,&array2,&array2Length)

However I don't like the notation and I believe there should be a simpler way of achieving what I want?

Share this post


Link to post
Share on other sites
Thaumaturge    3831
I forget whether they're part of pure C, but why not return a struct of arrays? Something along these lines (my C may be a little off, please note):


struct Array_return
{
float* array1;
int array1_len;

int* array2;
int array2_len;

//etc.
};

Array_return return_some_arrays( /*parameters here*/)
{
Array_return ret_val;

//Do whatever is called for, including placing relevant items into ret_val

return ret_val;
}

Share this post


Link to post
Share on other sites
Hodgman    51346
How about using the dereference operator instead of pretending that your pointers are arrays. Also, assert is nice in pointer-heavy code.
    assert( array1Length && array2Length );
if (*array1Length!=-1)
{
assert( array1 );
*array1=malloc( sizeof(float)*23234 );
*array1Length=23234;
}
if (*array2Length!=-1)
{
assert( array2 );
*array2=malloc( sizeof(int)*99 );
*array2Length=99;
}



EDIT: Incorporated Zahlman's observation ;)

[Edited by - Hodgman on August 19, 2008 9:15:42 PM]

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by floatingwoods
I need a pure C solution


Quote:
array1[0]=new float[23234];


Er, are you sure? [smile]

Share this post


Link to post
Share on other sites
bitshifter    113
How about using a list of pointers?

typedef struct {
void* data; /* An array of some data type. */
unsigned size; /* The array's max capacity. */
struct node_t* next; /* The next node in the list. */
} node_t;

Share this post


Link to post
Share on other sites
Zipster    2365
Quote:
Original post by floatingwoods
However I don't like the notation and I believe there should be a simpler way of achieving what I want?

I would say that what you have is the most acceptable solution, notation be damned. You're only going to clutter your code once you start creating special structures, or linked lists, or some other fancy, overkill solution just so you can return multiple values for this one function. Unless, of course, they actually belonged in a structure, or a linked list. Without any more context I'd stick with what you have.

Share this post


Link to post
Share on other sites
floatingwoods    364
Thanks for all the input!
Actually I am surprised there is no more elegant way..
The passing a structure solution might be more readable, but it needs more typing to call the function.
Zahlman, I didn't understand your comment?
oh and I prefer the notation array[0] than (*array), it is more intuitive for me to understand what is going on

Share this post


Link to post
Share on other sites
CmpDev    100
Quote:
Original post by floatingwoods
Zahlman, I didn't understand your comment?

He is making the observation that you are using C++ due to calling "new" and querying if indeed you need a "pure C solution".

Share this post


Link to post
Share on other sites
Antheus    2409
Quote:
Original post by floatingwoods
Thanks for all the input!
Actually I am surprised there is no more elegant way..


How about not returning anything?

What is it that "returnSomeArrays" really is?

Typically, the encapsulation works something like that:

void init_foo(foo_ptr ** p);
void manipulate_foo(foo_ptr * p);
void destroy_foo(foo_ptr ** p);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this