c – 将结构传递给函数

c – 将结构传递给函数,第1张

概述我正在练习不同的方法来为结构变量赋值.但当 我试过这个我得到一些垃圾的价值.这是代码. #include<iostream>using namespace std ;struct Distance{ int feet ; int inches;};void get_val(Distance ); void disp(Distance); 我正在练习不同的方法来为结构变量赋值.但当
我试过这个我得到一些垃圾的价值.这是代码.
#include<iostream>using namespace std ;struct distance{    int feet ;    int inches;};voID get_val(distance );               voID disp(distance);int main(){    distance l,m ;                   //Objects     get_val(l);    get_val(m);    disp(l);    disp(m);    return 0  ;}voID get_val(distance length){    cout<<"\nEnter Length "        <<"\nFeet  : ";    cin>>length.feet;    cout<<"\nInches : ";    cin>>length.inches;}voID disp(distance length){    cout<<"\nLength : \n"        <<"Feet : "<<length.feet        <<"\nInches : "<<length.inches ;}

为什么我得到垃圾值作为输出?

解决方法 通过参考.
voID get_val(distance&);voID get_val(distance &length){}

您在另一个函数中更改在main函数中创建的结构的值,因此您必须通过引用或使用指针来传递它们.

编辑:

感谢本杰明·林德利

总结

以上是内存溢出为你收集整理的c – 将结构传递给函数全部内容,希望文章能够帮你解决c – 将结构传递给函数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1249393.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存