简体   繁体   English

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

[英]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. 有没有可能将多个值从函数返回到main,以便我可以在main函数中单独使用。 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... 1)使用global variable ...但不是一个好习惯,我会说以上cmnt ...

2)Using and passing variables in argument by reference (ie call by reference ) . 2)在引用中使用和传递变量(即call by reference )。 EX: 例如:

void f(int *p , int *q)

3)having the return type as array ... or structure or union . 3)具有返回类型为array ...或structureunion 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... 4)将返回类型作为数组在处理同类数据类型甚至比使用struct更好时会很有用...因为使用array比使用struct更容易...主要优点...我们可以遍历它们如果需要在循环中使用计数器变量...也可以对返回的数据进行简单的数据操作...

I know C does not allow directly returning of arrays...for that you can do like the following "sample code" im 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)使用它们的混合形式... 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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