#Returning arrays

45 messages · Page 1 of 1 (latest)

uneven pulsar
#

Curious about whether I'm just doing it wrong or if there's a better method, but I'm not quite sure how to make a function return an array.

For some reason, it hasn't quite worked for me, but it seems to work okay if I make it save the results to a given array and just have the function be a void

Though even then, it still seems unfortunate because I would have to have the array size beforehand, which at times seems computationally redundant in more complex cases

Is there a better solution? I've not much experience with C so I could just have the syntax wrong but I couldn't really find anything on it online

dense crescentBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

uneven pulsar
#

(I'm probably skipping a few steps in terms of learning but that's a pretty big pattern with me)

valid jetty
#

a function can't return an array, and you've already discovered the usual solution of "save the results to a given array and have the function be void"

#

it is possible to have a function return a struct or union that contains an array, but you still need to know the size ahead of time

uneven pulsar
#

That's unfortunate

valid jetty
#

you can allocate different sized arrays with malloc, but this is often much more "computationally redundant"

#

malloc is more "expensive" than you would expect

uneven pulsar
#

Oh well

valid jetty
#

there are some other options that all have their own problems, C does not do a good job with "different sized arrays"

uneven pulsar
#

An example of a thing I was trying was having a function that takes an input char array and returns a 2d array containing all combinations of the elements in that char array

So for example, function("abc") would return { "abc", "acb", "bac", "bca", "cab", "cba" }

The size for a char array of size N would be [N][N!]
Factorial functions seemed expensive to calculate twice

#

I'll try to look into seeing if malloc would work for something like that I suppose

#

Thanks corHappy

#

!solved

dense crescentBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

uneven pulsar
valid jetty
uneven pulsar
#

Yeah
Is the amount of memory it uses linear?

valid jetty
#

linear in the size of the allocation, although it will at least be rounded up to a multiple of 16 and possibly rounded up to a power of 2

uneven pulsar
#

That sounds harsh

valid jetty
#

it's not so bad, it means it's always more than 50% efficient, plus it means memory comes in standard sized blocks so it's more likely to be reusable

uneven pulsar
#

Fair enough

valid jetty
#

i see a lot of beginners on here worried about putting five or six pointers on the stack and my advice to you is don't worry about that

#

basically anything smaller than 1K on stack is not worth worrying about

uneven pulsar
#

It might take me a while to get used to active memory management
I only started learning a couple hours ago tho so it might grow on me considering my C# habits

valid jetty
uneven pulsar
#

1M bytes?

#

I suppose I won't run out soon then

rich falcon
valid jetty
#

not many microcontrollers i know of that use C#

rich falcon
#

but a good reason to start using C/C++ from C# is to do microcontrollers.

uneven pulsar
#

I had learned js for use with microcontrollers ApolloSilly

runic patrol
uneven pulsar
#

The syntax is suddenly lost on me whyyyyyy

uneven pulsar
#

It looks a bit more like cpp than c

runic patrol
#

here is the C equivalent

valid jetty
#

that's C++ still

uneven pulsar
runic patrol
# valid jetty that isn't C
struct Arr1
{
  const char* elements[3];
};

struct Arr1 runFunc()
{
    struct Arr1 result = {{"test", "array", "return"}};
  return result;
}

int main()
{

}

really

lyric falcon
#

generic chained array system ```c
typedef struct s_Carray
{
void* data; //void* is a generic type you can cast it to get the data
struct s_Carray* next;
}*Carray;

Carray create()
{
Carray out = malloc(sizeof(struct s_Carray));
out->next = NULL;
out->data = NULL;
return out;
}
void free_Carray(Carray in)
{
Carray temp = in;
while(temp != NULL)
{
if(temp->data != NULL)
{
free(data);
}
Carray temp2 = temp;
temp = temp->next;
free(temp2);
}
}