本文介绍了全局结构声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 如果一个结构或其成员被传递给一个函数,那么它是否必须全局声明? #include< stdio.h> struct mystruct { int a; int b; }; int structfunc(struct mystruct foo); int main(void) { struct mystruct bar; bar.a = 123; bar.b = 456; int sum = structfunc(bar); printf( "%d \ nn",sum); 返回0; } int structfunc (struct mystruct foo) { int sum = foo.a + foo.b; 返还金额; } 如果在main()中移动struct 声明,则此代码无法编译。 解决方案 " Frane Roje" < frane.roje(d * elete *)@ st.hinet.hr>写在 新闻:bt ********** @ ls219.htnet.hr: 我从未见过声明在函数内部的结构。 我一直这样做。如果我需要在函数中打包一些常见的数据集 来使我更清楚。为什么我应该在函数外面公开 结构声明? 我相信它不常见,没有理由为什么有人在里面声明它一个功能。 是的,对其他功能没用。 看看你的热门帖子破坏了主题? - - 马克 - > - " Frane Roje" < frane.roje(d * elete *)@ st.hinet.hr>在消息中写道 news:bt ********** @ ls219.htnet.hr ... 好吧,我们都在这里学习,我很高兴我说出了我所说的''因为我现在知道更多了! 我使用VC7,我在main()中编写了一个带有struct的代码,它>没有出现在课堂视图中,是否出现在你的编辑器中?如果我声明一个全局结构,我确实在类视图中显示。 我忘了提到它确实编译并使用了一个结构内部 功能 - ------------------------- - 完成任务的时间! 删除(d * elete *)回复 还有一件事 int sum = structfunc(bar); 这段代码是C ++也许这就是为什么代码赢了'编译 - --------------------- ------ 完成任务的时间! 删除(d * elete *)回复 If a struct or its members are passedto a function, must it be declared globally?#include <stdio.h>struct mystruct{int a;int b;};int structfunc(struct mystruct foo);int main(void){struct mystruct bar;bar.a = 123;bar.b = 456;int sum = structfunc(bar);printf("%d\n", sum);return 0;}int structfunc(struct mystruct foo){int sum = foo.a + foo.b;return sum;}This code doesn''t compile if the structdeclaration is moved inside main(). 解决方案 "Frane Roje" <frane.roje(d*elete*)@st.hinet.hr> wrote innews:bt**********@ls219.htnet.hr: I''ve never seen a declaration of struct inside a function.I do it all the time. If I need to package up some common set of datainside a function to make it more clear I do. Why should I expose thestruct declaration outside the function? I believe it''s not common and there is no reason why should anyone declare it inside a function.Yes there is, it is no use to other functions.See how your top posting destroyed the thread?--- Mark ->--"Frane Roje" <frane.roje(d*elete*)@st.hinet.hr> wrote in messagenews:bt**********@ls219.htnet.hr... Well we''re all here to learn, I''m glad I said what I said ''cause now Iknow more!I use VC7 and i wrote a code with a struct inside main() and it >did notshow up in the class view, does it appear in your editor? If I declare a global struct i does show in the class view.I forgot to mention that it does complie and work with a struct insidefunction-----------------------------Time to get it done!Remove (d*elete*) to replyAnd 1 more thing int sum = structfunc(bar);This code is C++ maybe that is why the code won''t compile-----------------------------Time to get it done!Remove (d*elete*) to reply 这篇关于全局结构声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!