0

I want to return few values from method:

unsigned long long *operand1, *operand2;
unsigned long long value1;
int operation=0;

I was thinking about putting those datas in char array and then retrieve them byte after byte. Is this a good idea? How should I do this? Or could you suggest anything else?

DzikiChrzan
  • 2,747
  • 2
  • 15
  • 22
  • Why do you declare `*operand1, *operand2;` as pointers? c++ has by reference parameter support. – πάντα ῥεῖ May 03 '15 at 20:45
  • He said he uses C. Read the title. – adjan May 03 '15 at 20:48
  • 2
    @addy2012 He flagged it C++, so C++ answers answer this question. If he does not want C++ answers, he must remove the C++ tag. – Baum mit Augen May 03 '15 at 20:51
  • "I was thinking about putting those datas [sic] in char array and then retrieve them byte after byte. " - No, unnecessary type-punning is not a good idea. Also, are you using C or C++? *They're not the same thing.* – Iskar Jarak May 03 '15 at 20:52
  • Alright, reopen and then close as dupe of http://stackoverflow.com/questions/3829167/returning-multiple-values-from-a-function – Baum mit Augen May 03 '15 at 20:53
  • Sorry, I added C++ tag habitually. I'm using C and I thing that struct can handle this problem. – DzikiChrzan May 03 '15 at 20:53
  • I agree, define a struct, within an allocated memory area, then return a pointer to the struct/ I would also eliminate the 'pointer to long long entries by placing the actual value within the struct. however if the function is to return different types at different invocations, the use a struct that contains a union, and the first field indicates which type is actual being returned. – user3629249 May 03 '15 at 21:24

3 Answers3

2

I would suggest using a struct.

struct my_struct {
    unsigned long long *operand1, *operand2;
    unsigned long long value1;
    int operation=0;
};
adjan
  • 13,371
  • 2
  • 31
  • 48
2

If you are using C++11 then you can use std::tuple. See the reference http://en.cppreference.com/w/cpp/utility/tuple for examples of how to implement it.

This has the advantage over struct that you don't need to declare a new object for every set of types that you want to return.

Kvothe
  • 1,819
  • 2
  • 23
  • 37
1

With plain C code you probably want to have something like

typedef struct params_ {
    unsigned long long operand1, operand2;
    unsigned long long value1;
    int operation;
} params_t;

int foo(struct params_* p) {
    int retcode = 0;
    // do something with p along operation
    if(p->operation == 0) {
        p->value1 = 100 * p->operand1 + p->operand2;
    }
    else if(p->operation == 1) {
       // ...
    }
    // ...
    else {
        retcode = -1;
    }
    return retcode;
}

and use it like

int main(void) {
    params_t params;
    params.operation = 0;
    params.operand1 = 5;
    params.operand2 = 42;
    if(foo(&params) < 0) {
        // Error occurred
    }
    unsigned long long result = params.value1;
    return 0;
}

See the live demo here please.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • this line: 'int foo(struct params_t* p) {' does not compile. either remove the 'struct' modifier or (better) eliminate the 'typedef' and just declare a struct – user3629249 May 03 '15 at 21:21