returning multiple values from a function [duplicate]

Can anyone tell me how to return multiple values from a function?
Please elaborate with some example?


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;
}

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;
}

First of all, take a step back and ask why you need to return multiple values. If those values aren't somehow related to each other (either functionally or operationally), then you need to stop and rethink what you're doing.

If the various data items are part of a larger, composite data type (such as a mailing address, or a line item in a sales order, or some other type described by multiple attributes), then define a struct type to represent a single value of that composite type:

struct addr { // struct type to represent mailing address
  char *name;
  int streetNumber;
  char *streetName;
  char *unitNumber; 
  char *city;
  char state[3];
  int ZIP;
};

struct addr getAddressFor(char *name) {...}

struct point2D {
  int x;
  int y;
};

struct polygon2D {
  size_t numPoints;
  struct point2D *points;
};

struct point2D getOrigin(struct polygon2D poly) {...}

Do not define a struct to collect random items that aren't somehow related to each other; that's just going to cause confusion for you and anyone who has to maintain your code down the road.

If the data items are not functionally related, but are somehow operationally related (e.g. data plus a status flag plus metadata about the operation or items as part of a single input operation), then use multiple writable parameters. The most obvious examples are the *scanf() functions in the standard library. There are also the strtod() and strtol() functions, which convert a string representation of a number; they return the converted value, but they also write the first character that was not converted to a separate parameter:

char *str = "3.14159";
double value;
char *chk;

value = strtod(str, &chk);
if (!isspace(*chk) && *chk != 0)
  printf("Non-numeric character found in %s\n", str);

You can combine these approaches; here's an example inspired by some work I'm currently doing:

typedef enum {SUCCESS, REQ_GARBLED, NO_DATA_OF_TYPE, EMPTY, ERROR} Status;

typedef struct bounds {...} Bounds; 

tyepdef struct metadata {
  size_t bytesRead;
  size_t elementsRead;
  size_t rows;
  size_t cols;
} Metadata;

typedef struct elevations {
  size_t numValues;
  short *elevations;
} Elevations;

Elevations elevs;
Metadata meta;
Bounds b = ...; // set up search boundary

Status stat = getElevationsFor(b, &elevs, &meta);

The service that I request elevation data from returns a 1-d sequence of values; the dimensions of the array are returned as part of the metadata.