• โ™ฅ she/her โ™ฅ

cis woman. ecosocialist and urbanist.
โ–’โ–’โ–’โ–’โ–’
extremely gay (concerningly so)
โ–’โ–’โ–’โ–’โ–’
i write fiction and also do 3d art and progrmaming. am autistic.

Information wants to be free.

โ€‹

/-----------------------------/
โ€‹


how the fuck do i do this. it is literally. impossible. like not 'oh its possible but really hard' its impossible to fucking do this. I've looked through like 50k stack overflow posts and they all say you cant do this except with some weird combination of pointers and malloc. how do i do this. i can't just use void cuz according to my class im not meant to.


C is a demonic language made by demonic people. why can't i return arrays? nobody knows. why can't i return strings? oh because you can't return arrays. why does the program segfault when i try to use modulo on a double? well you see that's because there's another arcane function to do module on a double. why? nobody knows. it is Forbidden because it is. why do i get a segfault when i try to print a double? oh because there's no implicit typecasting even for print statements. what does 'expression must have integral type' mean? oh it means that you can't do this because we decided so.

call me a noob programmer, but i've been coding for like 6 years if not more now, since i was a kid, in LUA, JavaScript, Java, C#, Kotlin, Python, and Bash. this is the worst language i've touched. i want to wash my hands. yuck.


You must log in to comment.

in reply to @trainsfemme's post:

the two ways to do this is by either creating an array on the heap (using malloc()), or (imo the better method (in most circumstances)) giving the find_divisor function a pointer to an output array, which it will fill in the values for

something like this:

void find_divisor(int* inputArray, int** outputArray)
{
     // (do stuff with inputArray[i] to solve for divisor and dividend)
     *outputArray[0] = divisor;
     *outputArray[1] = dividend;
}

int main(int argc, char** argv)
{
     int data[] = {1, 2, 3, 4, 5, 5, 6};
     int result[2];
     find_divisor(data, &result);
     // (returned array is stored in result[])
     // ...
}

a lot of the reasons for the weird quirks in C are due to how CPUs actually work x_x i've managed to internalize all the reasonings

("why can't you just return an array?" because it either has to be allocated on the stack or the heap, but the local stack is cleared when your function returns, so it must be stored on the caller's stack, or in the heap, but it has to be later free()-ed if allocated in the heap)

edit: if your class is Requiring the return type to be non-void then i'm guessing they Expect you to allocate it using malloc() but that's kinda dumb :( bleh

wait you might be able to have the function return a struct? i never looked into that because i always just did this indirect access method, for Object Ownership reasons, plus returning a struct seems weird to me,,,

Either way, it would need to be heap-allocated because the local stack gets cleaned up when the function returns. So you'd still have to malloc space for it.

It might seem to work if you allocate it on the stack, but that memory could be reused at any time.

usually but not always, for example if you have a struct on the stack of the main function (or whatever root function), you can always pass a struct pointer to functions and it won't get cleared away after they return, since the main function holds onto it

it Would clear if the main function returns, but that only happens when the program finishes running, so it's not much of a concern ๐Ÿ‘

many libraries already do this, by instructing the programmer to allocate a state struct and re-use it for all the function calls, which can sit on the main function's stack, or the heap (and keeping it on the stack (if you can) is simpler/safer)

i have no idea how Returning A Struct works in this model though so i don't really do it (i would Hope that the caller makes space on the stack for the return value to be stored into? below the return address so it stays in memory even after the callee returns)

i obsess over C too much as you can probably tell D:

Oh, well, yeah, you could pass a pointer to a stack-allocated struct, but at that point why not just pass the pointer to your array?

(I too obsess, but mostly over C++ ๐Ÿ˜)

(I'm talking about the System V x86_64 ABI, I don't know about other platforms) Returning big structs is done by the adding an implicit outparam, the caller will reserve and release the memory automatically (It will use the stack but nothing prohibits a compiler to use malloc/free implicitly AFAIK).

Returning structs used to be unsupported but enough compilers added it as an extension that I believe it's in the standard now. It's not heap allocated, it gets copied into the stack frame of the caller

Returning a struct works.

We can debate on if returning a dynamically sized array is valuable (it IS possible to do). But the fact that returning an int[3] does not work is just nonsense.

Since the array has a fixed size, you can wrap it in a struct... Dn't ask why it has to be wrapped in a struct..

struct find_devisor_result {
  int result[2];
};

struct find_devisor_result
find_devisor(...) {
  // return 1 & 2 always yes very algorithm
  return (struct find_devisor_result) { .result = { 1, 2 } };
}

(i also didn't test this code at all so it might just not work)

I would say skip the question and ask the teacher.

but https://stackoverflow.com/questions/11656532/returning-an-array-using-c might be helpful. And you can find the size of the array in the place you find the initial array, then pass the size as a parameter.

some teachers use initial quizzes to judge the knowledge of the class in general, and this might be it. Otherwise, they can at least point you to resources.

the struct answer probably is more correct, but may not be for your class if they haven't taught you structs exist yet.

Embrace the darkside. Let the madness take hold.

From what I can tell, this function takes an array of two numbers only. You allocate an array of 2 numbers on the stack, and pass a pointer to it to your function. And then you just write to that pointer within the function.

In general you never want to use malloc within a function. Ideally, you only want to allocate memory at the top scope, and pass a pointer to that memory to whatever function that needs it.

When I was doing C, I remember using structs a lot. For this assignment, probably a struct would be better.

struct two_numbers = {2, 4}
two_numbers = find_divisor(two_numbers)

The larger problem with this question is that since it is impossible to actually return a [local] array, it is unclear which of the things that are possible that it wants you to do. If I were given this question I would probably just use a static array, since that comes closest to the instructed 'return an array' without being undefined behavior.

Pinned Tags