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

Started by
7 comments, last by Antheus 15 years, 8 months ago
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?
Advertisement
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; }

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

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]
Quote:Original post by floatingwoods
I need a pure C solution


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


Er, are you sure? [smile]
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;
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.
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
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".
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);

This topic is closed to new replies.

Advertisement