Public Group

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

This topic is 3739 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
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 on other sites
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 on other sites
Quote:
 Original post by floatingwoodsI need a pure C solution

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

Er, are you sure? [smile]

##### Share on other sites
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 on other sites
Quote:
 Original post by floatingwoodsHowever 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 on other sites
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 on other sites
Quote:
 Original post by floatingwoodsZahlman, 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 on other sites
Quote:
 Original post by floatingwoodsThanks for all the input!Actually I am surprised there is no more elegant way..

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);

1. 1
Rutin
31
2. 2
3. 3
4. 4
5. 5

• 12
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633313
• Total Posts
3011322
• ### Who's Online (See full list)

There are no registered users currently online

×