C:如何将多个值从函数返回到main

时间:2021-07-16 19:23:17

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.

是否有可能将多个值从函数返回到main,以便我可以在main函数中单独使用。我需要在我正在开发的项目中使用这个概念。由于代码很大,我给出了一个显示我的要求的简单代码。

#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

我邀请多种解决方案,但请用适当的代码解释您的概念。感谢你的时间

3 个解决方案

#1


4  

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

#2


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...

1)使用全局变量...但不是一个好的做法我会说如上面的cmnt ...

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

2)通过引用在参数中使用和传递变量(即通过引用调用)。 EX:

void f(int *p , int *q)

void f(int * p,int * q)

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

3)将返回类型作为数组...或结构或联合。例如:

temp f(<the parameters here>);

temp f( <这里的参数> );

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...

4)当你处理同类数据类型甚至比使用struct更好时,返回类型作为数组会很有用...因为使用数组比使用struct更容易...主要优点...我们可以迭代它们如果需要在循环中使用计数器变量...并且还可以对返回的数据进行简单的数据处理...

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

我知道C不允许直接返回数组...因为你可以像我在这里发布的下面的“示例代码”那样做

`

`

#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...

5)使用它们的混合混合......就像返回结构的指针一样......将参数作为结构数据类型的引用传递...

#3


-2  

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.

#1


4  

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

#2


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...

1)使用全局变量...但不是一个好的做法我会说如上面的cmnt ...

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

2)通过引用在参数中使用和传递变量(即通过引用调用)。 EX:

void f(int *p , int *q)

void f(int * p,int * q)

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

3)将返回类型作为数组...或结构或联合。例如:

temp f(<the parameters here>);

temp f( <这里的参数> );

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...

4)当你处理同类数据类型甚至比使用struct更好时,返回类型作为数组会很有用...因为使用数组比使用struct更容易...主要优点...我们可以迭代它们如果需要在循环中使用计数器变量...并且还可以对返回的数据进行简单的数据处理...

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

我知道C不允许直接返回数组...因为你可以像我在这里发布的下面的“示例代码”那样做

`

`

#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...

5)使用它们的混合混合......就像返回结构的指针一样......将参数作为结构数据类型的引用传递...

#3


-2  

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.