returning multiple values from a function

You can't do that directly. Your options are to wrap multiple values into a struct, or to pass them in as pointer arguments to the function.

e.g.

typedef struct blah
{
    int a;
    float b;
} blah_t;


blah_t my_func()
{
    blah_t blah;
    blah.a = 1;
    blah.b = 2.0f;
    return blah;
}

or:

void my_func(int *p_a, float *p_b)
{
    *p_a = 1;
    *p_b = 2.0f;
}

Your choices here are to either return a struct with elements of your liking, or make the function to handle the arguments with pointers.

/* method 1 */
struct Bar{
    int x;
    int y;
};

struct Bar funct();
struct Bar funct(){
    struct Bar result;
    result.x = 1;
    result.y = 2;
    return result;
}

/* method 2 */
void funct2(int *x, int *y);
void funct2(int *x, int *y){
    /* dereferencing and setting */
    *x  = 1;
    *y  = 2;
}

int main(int argc, char* argv[]) {
    struct Bar dunno = funct();
    int x,y;
    funct2(&x, &y);

    // dunno.x == x
    // dunno.y == y
    return 0;
}

Tags:

C