Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
It looks like filter() effectively takes two arguments; the array to be filtered, and a predicate expression resulting in a boolean indicating whether the entry should be included in the output or ...
Answer
#1: Initial revision
It looks like `filter()` effectively takes two arguments; the array to be filtered, and a predicate expression resulting in a boolean indicating whether the entry should be included in the output or not. (I also note that the implementation in your question appears to be at least O(n^2) time complexity, which is pretty bad for anything but the smallest arrays.) Beginning at the point of not concerning oneself with source code byte count, the tool in C's toolbox that I would reach for for this is either a specialized function for this one usage, or one that takes a pointer to a function that serves the purpose of the filter predicate. The latter is slightly more complex, but also generic as it can accept any predicate function. The function pointer syntax in C takes some getting used to. First, write a function that does whatever filtering you want. (For a real-world application, unless I had some highly specialized need, I would first see if there are already-debugged third-party library implementations of this rather than trying to come up with my own implementation.) int* filter(int* input, bool (*predicate)(int)) { int* output; while (*input != -1) { if(predicate(*input)) { // somehow include *input in the output } input++; } return output; } Then, write a predicate function which will be called once per entry: bool my_predicate(int value) { return (value & 3) != 0; // or whatever } Then put them together: int* filtered = filter(unfiltered, my_predicate); Adding some boilerplate this compiles cleanly with [`gcc -Wall -std=c99 -pedantic-errors -Wextra -Werror`](https://software.codidact.com/posts/282565/282566#answer-282566). Reducing the character count in the source code is kind of what code golfing is about as I understand it, so I will leave that part entirely up to you.