-2

First of all, I know that passing by reference is used when we want to return more values in c, but in this specific case I want to make somehow possible for the user to see which are available returns. Should I store values in array, or what could be a good solution?

char function(char operation[], int given_pos, int given_ran, int limit)
{
  
  if((given_pos+given_ran)>0 && (given_pos+given_ran)<limit)
  {
    return '+';
  }
  if((given_pos-given_ran)>0 && (given_pos-given_ran)<limit)
  {
    return '-';
  }
  if((given_pos*given_ran)>0 && (given_pos*given_ran)<limit)
  {
    return '*';
  }
  if((given_pos/given_ran)>0 && (given_pos/given_ran)<limit)
  {
    return '/';
  }
  return 0;
}

In parameters, char operation[] is given, because I call this term function in another function. Everything works fine, but I want to make it possible for the user to know all the operations that are possible. It's not working because every time '+' is available, others are not, etc.

fm_
  • 19
  • 1
  • 4
  • @hyde here is updated code, any ideas? – fm_ May 09 '22 at 11:09
  • Idea: Use more `else` for clarity. – Yunnosch May 09 '22 at 11:41
  • 1
    If you want to return something like "the following operations are valid: + and /" I would suggest a structure (possibly an array) of booleans/bits. – Yunnosch May 09 '22 at 11:44
  • @Yunnosch what do you mean by that structure? – fm_ May 09 '22 at 11:48
  • A `struct`, with booleans (whatever your environment offers) or bits as implemented by the bitfield syntax. You do know how to make a `struct`, don't you? – Yunnosch May 09 '22 at 11:52
  • Of course, but the problem for me is implementing that array from struct – fm_ May 09 '22 at 11:54
  • 1
    Does this answer your question? [How do I return multiple values from a function in C?](https://stackoverflow.com/questions/2620146/how-do-i-return-multiple-values-from-a-function-in-c) – mmixLinus May 09 '22 at 12:09
  • Either array or struct. No need for array of structs. – Yunnosch May 09 '22 at 12:20
  • Your function does seem like it does two things, validates parameters and returns list of valid operations? Maybe you want `const char *validOperations() { return "+-*/"; }` and validation `bool validateSomething(int given_pos, int given_ran, int limit) { return (given_pos+given_ran)>0 && (given_pos+given_ran) – hyde May 09 '22 at 12:39
  • I like the "is this valid?" approach. @hyde Any calling code could then poll, which I suspect it would otherwise do anyway on the all-in-one-return-value anyway. – Yunnosch May 09 '22 at 12:41
  • @hyde could you please explain more your approach in answers, it seems like its something I search for. Thanks in advance – fm_ May 09 '22 at 13:13
  • @fm_ The question is still not clear to me. – hyde May 09 '22 at 14:01
  • @hyde Yes I have function "validate" for validation. I have posted how it looks like. In another function called "operation", I am passing operation (char) from user to function "validate", which is giving me back possible operations. How should that look like if I have more than 1 possible operation. Hope its clear now – fm_ May 09 '22 at 14:09

1 Answers1

1

You could place each possible operator in a provided string buffer (or return a dynamically allocated buffer).

#include <stdio.h>

int in_range(int l, int v, int u) {
    return l < v && v < u;
}

size_t func(char *ops, int given_pos, int given_ran, int limit) {
    size_t n = 0;

    if (in_range(0, given_pos + given_ran, limit))
        ops[n++] = '+';

    if (in_range(0, given_pos - given_ran, limit))
        ops[n++] = '-';

    if (in_range(0, given_pos * given_ran, limit))
        ops[n++] = '*';

    if (in_range(0, given_pos / given_ran, limit))
        ops[n++] = '/';

    ops[n] = 0;

    return n;
}

int main(void) {
    char buf[32];
    size_t number_of_ops = func(buf, 32, 16, 64);

    printf("%zu ops: [%s]\n", number_of_ops, buf);
}

stdout:

3 ops: [+-/]

Or you could return a structure, if you prefer the code verbosity. This is more meaningful when the "return values" differ in type.

#include <stdbool.h>
#include <stdio.h>

struct ops {
    bool add;
    bool sub;
    bool mul;
    bool div;
};

bool in_range(int l, int v, int u) {
    return l < v && v < u;
}

struct ops func(int given_pos, int given_ran, int limit) {
    struct ops o = {0};

    o.add = in_range(0, given_pos + given_ran, limit);
    o.sub = in_range(0, given_pos - given_ran, limit);
    o.mul = in_range(0, given_pos * given_ran, limit);
    o.div = in_range(0, given_pos / given_ran, limit);

    return o;
}

int main(void) {
    struct ops o = func(32, 16, 64);

    printf("add? %d sub? %d mul? %d div? %d\n", o.add, o.sub, o.mul, o.div);
}

stdout:

add? 1 sub? 1 mul? 0 div? 1

Or you could use bitwise operations to create a basic set of bitmasks, allowing use of an integral type to represent the options available.

#include <stdio.h>

#define CAN_ADD (1 << 0)
#define CAN_SUB (1 << 1)
#define CAN_MUL (1 << 2)
#define CAN_DIV (1 << 3)

int in_range(int l, int v, int u) {
    return l < v && v < u;
}

unsigned func(int given_pos, int given_ran, int limit) {
    unsigned o = 0;

    if (in_range(0, given_pos + given_ran, limit))
        o |= CAN_ADD;

    if (in_range(0, given_pos - given_ran, limit))
        o |= CAN_SUB;

    if (in_range(0, given_pos * given_ran, limit))
        o |= CAN_MUL;

    if (in_range(0, given_pos / given_ran, limit))
        o |= CAN_DIV;

    return o;
}

int main(void) {
    unsigned o = func(32, 16, 64);

    printf("add? %d sub? %d mul? %d div? %d\n", o & CAN_ADD, o & CAN_SUB,
           o & CAN_MUL, o & CAN_DIV);
}

stdout:

add? 1 sub? 2 mul? 0 div? 8
Oka
  • 23,367
  • 6
  • 42
  • 53
  • These are examples of ways to "return multiple values", with regards to the `function` function you have shown. You have *not* shown any "validate" or "operation" functions. Please post a more accurate [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) if you need help with a more specific problem. – Oka May 09 '22 at 15:27
  • Thank you so much!! Do you have any references where I can learn more about string buffer stuff. Solved problem!! – fm_ May 09 '22 at 15:34
  • Find a relatively modern, beginner [C textbook](https://stackoverflow.com/a/562377/2505965). – Oka May 09 '22 at 15:39