简体   繁体   中英

C: How to return separate multiple values from function to main

Is there any possibility to return multiple values from function to main so that i can use separately in main function. I need to use this concept in a project I am working on. As that code is huge, I am giving a simple code showing my requirements.

#include <stdio.h>

int func ()
{
    int a[3] = { 31, 32, 33};
    static int x, y, z;
    char b[20];

    x = a[0];
    y = a[1];
    z = a[2];

    printf ("%d\n", x);
    printf ("%d\n", y);
    printf ("%d\n", z);

    return 0;
}

int main()
{
    int x, y, z ;

    func ();

    printf ("%d\n", x);
    printf ("%d\n", y);
    printf ("%d\n", z);

    return 0;
}

I invite multiple solutions, but please do explain your concept with proper code. Appreciate your time

You can use structures.

#include <stdio.h>

struct data_t {
    int x, y, z;
};

struct data_t func (void)
{
    int a[3] = { 31, 32, 33};
    struct data_t data;

    data.x = a[0];
    data.y = a[1];
    data.z = a[2];

    printf ("%d\n", data.x);
    printf ("%d\n", data.y);
    printf ("%d\n", data.z);

    return data;
}

int main(void)
{
    struct data_t data;

    data = func ();

    printf ("%d\n", data.x);
    printf ("%d\n", data.y);
    printf ("%d\n", data.z);

    return 0;
}

Alternative way using pointers:

#include <stdio.h>

void func (int* x, int* y, int* z)
{
    int a[3] = { 31, 32, 33};

    *x = a[0];
    *y = a[1];
    *z = a[2];

    printf ("%d\n", *x);
    printf ("%d\n", *y);
    printf ("%d\n", *z);
}

int main(void)
{
    int x, y, z;

    func (&x, &y, &z);

    printf ("%d\n", x);
    printf ("%d\n", y);
    printf ("%d\n", z);

    return 0;
}

Briefly...You can do that by many methods below which is described:-

1) Using global variable ...but not a good practice i would say as the above cmnt...

2)Using and passing variables in argument by reference (ie call by reference ) . EX:

void f(int *p , int *q)

3)having the return type as array ... or structure or union . Ex:

temp f(<the parameters here>);

4)having return type as a array would be useful when you are dealing with homogeneous data types even better than using struct...since using array is more easy then using struct...the main advantage... we could iterate through them if needed using an counter variable in a loop...and also can do easy data manipulation of your returned datas...

I know C does not allow directly returning of arrays...for that you can do like the following "sample code" im posting down here

`

#include <stdio.h>

/* function to generate and return random numbers */
int * getRandom( ) {

   static int  r[10];
   int i;

   /* set the seed */
   srand( (unsigned)time( NULL ) );

   for ( i = 0; i < 10; ++i) {
      r[i] = rand();
      printf( "r[%d] = %d\n", i, r[i]);
   }

   return r;
}

/* main function to call above defined function */
int main () {

   /* a pointer to an int */
   int *p;
   int i;

   p = getRandom(); // this is the line where i want your attention to be

   for ( i = 0; i < 10; i++ ) {
      printf( "*(p + %d) : %d\n", i, *(p + i));
   }

   return 0;
}

`

5)Using Hybrid mixture of them... like returning a pointer of a structure ... passing argument as call by reference for a struct data type ...

int *func() 
    int a[3] = {31, 32, 33};
int x,y,z;

    x = a[0];
    y = a[1];
    z = a[2];

    printf ("%d\n", x);
    printf ("%d\n", y);
    printf ("%d\n", z);

    return a;
}

int main()
{
    int x, y, z;
    int b[3]; 

    b=func ();

    printf ("x:%d\n", b[0]);
    printf ("y:%d\n", b[1]);
    printf ("z:%d\n", b[2]);

    return 0;
}


I have not compiled this but I think it might help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM