如何正确调用struct return作为其他struct函数的输入? [英] How to correctly call struct return as input for other struct functions ?

查看:179
本文介绍了如何正确调用struct return作为其他struct函数的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将函数A的返回值传递给函数B,并将函数B的返回值传递给函数C.我正在使用struct。我可以从struct传递给函数然后传递main,但结果我仍然混淆了如何将struct return传递给一个返回另一个struct的函数。





当我把第一个函数作为第二个函数的输入时,它预期输入(实际上第一个函数需要输入)但是我想要调用返回值而不是函数。我很困惑。有什么建议?谢谢



我尝试过:



这是我定义的方式我的结构

 struct Credit {
float A;
浮动B;
浮动C;
};

struct中级{
浮点数学;
float bio;
};





然后我有我的第一个功能,来自用户的手动输入值。从这个函数我返回A,B和C的值

 struct Credit course(float x1,float y1,float x2,float y2)
{
//简单算术计算...

struct Credit结果;
result.A = A;
result.B = B;
result.C = C;
返回结果;
}



在我的第二个函数中,我需要从上一个函数中获取A,B,C的输入。然后返回math,bio作为下一个函数的输入值。

 struct中级(struct Credit point)
{
//简单计算涉及前一个函数(A,B,C)的返回结果
//我使用point.A,point.B和point.C

struct调用该值中间结果;
result.math = math;
result.bio = bio;
返回结果;
//此函数的结果将用于下一个函数
}



这是我的主要内容:

场(12,10,10,8); 
年级(课程());
//使用功能等级输入的下一个函数。

解决方案

忽略你提供的代码获胜的事实' t编译...



你的函数 course()返回一个结构,但是没有在你提供的 main()中分配一下。回到基础 - 假设我们有一个函数f,它接受一个int作为参数,并返回一个int值:

  int  f( int  x);  / *  声明一个带有int的函数,它返回一个int * /  

f ( 2 ); / * 以值2作为参数调用f - 抛弃返回值* /
f (); / * 错误:没有给f()的参数* /

< span class =code-keyword> int i;
i = f( 2 ); / * 确定:将返回值从f()分配给i * /
printf(< span class =code-string> i =%d \ n,i); / * 我们可以稍后使用i * /
printf( f(3)=%d \ n,f( 3 < /跨度>)); / * OK:f()的返回值传递给printf()* /



在您的情况下,您希望执行以下任一操作:

  struct  Credit MyCredit; 
struct 中级MyIntermediate;
MyCredit = course( 12 10 10 8 );
MyIntermediate = grade(MyCredit);

  struct 中级MyIntermediate =年级(学分(< span class =code-digit> 12 , 10  10  8 )); 



如果您不需要使用返回值,则可以使用第二个示例(稍后再说。



但是,通常情况下,我们不会将函数传递给函数或从函数返回结构。这是因为C使用pass-by-value,这意味着当您传递或返回结构时,编译器会根据需要将结构复制到堆栈或从堆栈复制。对于小结构,这通常不是问题。但是如果你有一个占用几千字节(甚至兆字节!)的大型结构,那么你就是为每个函数调用或返回复制那些数据。另一种方法可能

  void  course( struct  Credit * credit, float  x1, float  x2,< span class =code-keyword> float  y1, float  y2)
{
credit-> A = x1 + X2;
/ * 以及其他信用成员等等* /
}

int main()
{
struct Credit MyCredit;
课程(& MyCredit, 12 10 10 8 ); / * 将MyCredit的地址传递给course()* /
/ * 此处有更多代码... * /
return 0 ;
}



在这种情况下,我们永远不会添加超过指针大小(通常为32位系统为4个字节,64位系统为8个字节)到了堆栈。


虽然解决方案1本质上是正确的,但这是我要改变的,使你的代码不仅可以工作,而且还可以正常工作:

< pre lang =c ++> struct 学分课程(...) // 保留此函数
...
struct 中级( struct Credit& credit){ // 通过引用传递!
... // 保留功能代码
}
< span class =code-keyword> int main(){
Intermediate my_grade = grade(course( 12 10 10 8 ));
...
}



说明:

- 对课程的调用构造一个Credit类型的结构并返回它是
- 编译器确保在本地创建的结构被重用而不是复制;创建副本没有性能损失!

- 对此Credit结构的引用直接传递到调用堆栈以调用grade();再次,不需要副本 - 只传递一个引用

- 像course(),grade()返回内部构造的Intermediate结构并返回它而不需要副本



其他好处:无需在main中声明Credit类型的变量 - 如果您决定更改返回类型,则无需更改主函数!

Hi, I want to pass the return value of function A to function B, and pass the return value of function B to function C. I'm using struct. I can pass from struct to function and then main, but turn out I'm still confuse how to pass struct return to a function which return another struct as well.


When I put the first function as input of the second function, it expected input (indeed the first function needs input) but I wanted to call the return value not the function. I'm confuse. Any advice ? Thank you

What I have tried:

Here's how I define my struct

struct Credit {
    float A;
    float B;
    float C;
};

struct Intermediate {
   float math;
   float bio;
};



Then I have my first function, with manual input value from the user. And from this function I return the value of A, B and C

struct Credit course (float x1, float y1, float x2, float y2)
{ 
   // simple arithmetic calculation...
 
    struct Credit result;
    result.A = A;
    result.B = B;
    result.C = C; 
    return result;
}


In my second function, I need to get input from A,B,C from the previous function. And then return the value of math, bio as input from next function.

struct Intermediate grade (struct Credit point)
{
   // simple calculation involving return result of previous function (A, B, C)
   // I call the value using point.A , point.B and point.C
    
    struct Intermediate result;
    result.math = math;
    result.bio = bio;
    return result;
    //result from this function will be used for the next function
}


Here's my main :

course(12,10,10,8);
grade(course());
//next function using input from function grade.

解决方案

Overlooking the fact that the code you've provided won't compile at all ...

Your function course() returns a struct, but that doesn't get assigned in the little bit of main() you've provided. Going back to basics - suppose we have a function f that takes an int as an argument, and returns an int value:

int f(int x); /* declares a function taking an int which returns an int */

f(2);  /* calls f with value 2 as the parameter - return value is thrown away */
f();   /* ERROR: no parameter given to f() */

int i;
i = f(2);                    /* OK: assigns return value from f() to i */
printf("i = %d\n", i);       /* we can then use the i later */
printf("f(3) = %d\n", f(3)); /* OK : return value of f() is passed to printf() */ 


In your case, you want to do either of the following:

struct Credit MyCredit;
struct Intermediate MyIntermediate;
MyCredit = course(12, 10, 10, 8);
MyIntermediate = grade(MyCredit);

or

struct  Intermediate MyIntermediate = grade(credit(12, 10, 10, 8));


You would use the second example if you didn't need to use the return value of course() again later.

Normally, though, we do not pass or return structs to or from functions. That's because C uses pass-by-value, which means that when you pass or return a struct the compiler copies the struct to/from the stack as needed. For a small struct, that's generally not a problem. But if you have a large struct that takes up several kilobytes (or even megabytes!), you're copying that data for each function call or return. An alternative approach might be:

void course(struct Credit *credit, float x1, float x2, float y1, float y2)
{
   credit->A = x1+x2;
   /* and so on for other members of credit */
}

int main()
{
   struct Credit MyCredit;
   course(&MyCredit, 12, 10, 10, 8); /* passes address of MyCredit to course() */
   /* more code here ... */
   return 0;
}


In this case, we never add more than the size of a pointer (normally 4 bytes for 32 bit system, 8 bytes for 64 bit systems) to the stack.


Although solution 1 is essentially correct, this is what I would change, to make your code not only work, but work well:

struct Credit course(...) // leave this function as is
...
struct Intermediate grade(struct Credit& credit) { //pass by reference!
 ... // leave the function code as is
}
int main () {
   Intermediate my_grade = grade(course(12, 10, 10, 8));
   ...
}


Explanation:
- the call to course constructs a struct of type Credit and returns it
- the compiler makes sure that the struct which was created locally is reused rather than copied; there is no performance hit for creating a copy!
- a reference to this Credit struct is directly passed into the call stack for the call to grade(); again, no copy is needed - only a reference is passed
- like course(), grade() returns the internally constructed Intermediate struct and returns it without a copy

Other benefit: no need to declare a variable of type Credit in main - if you ever decide to change the return type, you don't need to change your main function!


这篇关于如何正确调用struct return作为其他struct函数的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆