简体   繁体   English

从函数返回值

[英]Returning values from function

I am very new to c programming. 我对C编程非常陌生。 I have written the fallowing code 我写了休闲代码

 float value;  //golbal variable
 unsigned int data; //golbal variable

 void Maxphase(void) 
 { 
  float  MAX = 0.0;
   unsigned int i,index;
 for (i=0;i<=360;i++) 
 {              
    phaseset(i); 
    data = readvalue(); 
    value = voltage(data);    
    if(value>MAX)   //find max value 
    { 
      MAX = value;    //max voltage 
      index = i;   
     }  
  }                          
    printf("Max Voltage Value:%f\r\n", MAX); 
    printf("Related index Value:%d\r\n", index); 
} 

the above code working perfectly and printing maximum voltage and index. 上面的代码可以正常工作,并打印最大电压和索引。 I want return both values "Max" and "index" from this function and I have to save Max value in one variable and index value in other variable like. 我想从此函数返回值“ Max”和“ index”,我必须将Max值保存在一个变量中,将索引值保存在其他变量中。

void runCom(void){ 
     c=getchar();
 switch(c){ 
    case '1': 
           Maxphase();
          Vin= (I want to store MAX value of that function)
          p1= ( I want to store Index of that function)
          break; 
    default:
         break;
    }
  }

Actually I want call that function and it has to return two variables MAX and index value, thus I want to store those two values in different variables. 实际上,我想调用该函数,并且它必须返回两个变量MAX和索引值,因此我想将这两个值存储在不同的变量中。

I know function can't return two values. 我知道函数不能返回两个值。

I have searched, i found it is possible with a struct or make the function to handle the arguments with pointers. 我进行了搜索,发现有可能使用struct或使函数能够处理带有指针的参数。 I tried with struct as shown below. 我尝试使用struct,如下所示。

typedef struct {
   float v;
   unsigned int p;
  }volphase;

I have declared this struct in header file. 我已经在头文件中声明了这个结构。 I am including this header file in all files where i am calling. 我在所有要调用的文件中都包含此头文件。

volphase Maxphase()
{
   volphase vp;
float  MAX = 0.0;
   unsigned int i,index;
 for (i=0;i<=360;i++) 
 {              
    phaseset(i); 
    data = readvalue(); 
    value = voltage(data);    
    if(value>MAX)   //find max value 
    { 
      MAX = value;    //max voltage 
      index = i;   
     }  
  }                          
   vp.v=MAX;
   vp.p=index; 
   return vp;
  } 

This is written in "bvr.c" file. 这写在“ bvr.c”文件中。 But I am thinking how to call this "struct" in case'1'(main.c) and how to store vp.v in one variable and vp.p in another variable. 但是我正在考虑如何在case'1'(main.c)中调用此“结构”,以及如何将vp.v存储在一个变量中并将vp.p存储在另一个变量中。

Please suggest me if any thing wrong in writing struct. 如果结构写错了,请提出建议。 or any other easiest way that will return two values. 或其他任何最简单的方法将返回两个值。

please help me how to do this. 请帮我怎么做。

Returning a struct from the function is the least common of the two ways to return multiple values. 从函数返回struct是返回多个值的两种方法中最少的一种。 Using pointers is more common: 使用指针更为常见:

void Maxphase(float *max, unsigned int *index) 
{
    *max = 0.0;
    float value;
    unsigned int i, data;
    for (i=0;i<=360;i++) 
    {              
        phaseset(i); 
        data = readvalue(); 
        value = voltage(mux1);    
        if(value > *max)   //find max value 
        { 
            *max = value;    //max voltage 
            *index = i;   
        }  
    }                          
    printf("Max Voltage Value:%f\r\n", *max); 
    printf("Related index Value:%d\r\n", *index); 
}

Here is how you call this function: 这是调用此函数的方式:

int main() {
    float max;
    unsigned idx;
    Maxphase(&max, &idx);
    printf("Max Voltage Value:%f\r\n", max); 
    printf("Related index Value:%d\r\n", idx); 
    return 0;
}

I would return the phase from the function - the function name is MaxPhase, which implies that it returns a value of maximum phase. 我将从函数返回相位-函数名称为MaxPhase,这意味着它返回最大相位值。 The index at which it found the max can be returned using a pointer. 可以使用指针返回找到最大值的索引。

Note that the data value is unused and mux1 is undefined. 请注意, data值未使用,而mux1未定义。 Note also that I used idx instead of index as the latter is sometimes already defined in standard libraries (although perhaps not in yours). 还要注意,我使用idx代替了index因为后者有时已经在标准库中定义了(尽管可能不是在您的库中定义的)。

float MaxPhase(int * maxindex)
{
    float max = 0.0;
    int idx = -1;

    for (int i=0; i<=360; i++) {
        phaseset(i);
        unsigned int data = readvalue();
        float value = voltage(mux1);
        if (value > max) {
            max = value;
            idx = i;
        }
    }
    *maxindex = idx;
    return max;
}

void caller(void)
{
    int idx = 0;
    float phase = MaxPhase(&idx);

    printf("Max Voltage Value:%f\n", phase);
    printf("Related index Value:%d\n", idx);

    ...
}
#include <stdio.h>

struct lol
{
    float val;
    int ind;
};

void olol(struct lol *lol1)
{
    lol1->val = 5;
    lol1->ind = 3;
}

int main(void) {
    struct lol mylol = {0,0};
    olol(&mylol);
    printf("lololol %f %d \n", mylol.val, mylol.ind);
    printf("lol\n");
    return 0;
}

You can solve this in several different ways: 您可以通过几种不同的方式解决此问题:

1.define a struct containing the two values and returning that struct. 1.定义一个包含两个值的结构并返回该结构。

#include <stdlib.h>
struct Values {
  int v1, v2;
};
struct Values *get2Values () {
  struct Values *x=(struct Values*)malloc (sizeof (struct Values));
  x->v1=1;
  x->v2=1231;
  return x;
}

and voila, you have a memory leak if you dont treat the returned value right... 瞧,如果不正确对待返回的值,就会发生内存泄漏。

2.use pointers as parameters where the values will go, eg Pointers: 2.使用指针作为值将要到达的参数,例如,指针:

void get2Values (int *v1, int *v2) {
  *v1=1;
  *v2=131231;
 }
 int main () {
   int a1, a2;
   get2Values (&a1, &a2);
 }

Good luck! 祝好运!

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

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