C语言 在哪里声明结构,在 main() 内部还是在 main() 外部?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31380268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
where to declare structures, inside main() or outside main()?
提问by Manik Arora
Case 1:structure declared outside main()working fine
案例 1:在main()正常工作之外声明的结构
#include<stdio.h>
#include<conio.h>
struct prod
{
int price,usold;
};
int main()
{
struct prod *p,a;
int billamt(struct prod *);
int bill;
printf("enter the values \n");
scanf("%d%d",&p->price,&p->usold);
bill=billamt(p);
printf("bill=%d",bill);
getch();
}
int billamt(struct prod *i)
{
int b;
b=(i->price*i->usold);
return b;
}
Case 2:declared inside main()giving error
情况 2:在内部声明main()错误
[Error] type 'main()::prod' with no linkage used to declare function 'int billamt(main()::prod*)' with linkage [-fpermissive]*
[错误] 类型 'main()::prod' 没有链接用于声明函数 'int billamt(main()::prod*)' 与链接 [-fpermissive]*
#include<stdio.h>
#include<conio.h>
int main()
{
struct prod
{
int price,usold;
};
struct prod *p,a;
int billamt(struct prod *);
int bill;
printf("enter the values \n");
scanf("%d%d",&p->price,&p->usold);
bill=billamt(p);
printf("bill=%d",bill);
getch();
}
int billamt(struct prod *i)
{
int b;
b=(i->price*i->usold);
return b;
}
回答by Sourav Ghosh
where to declare structures, inside
main()or outsidemain()?
在哪里声明结构,内部
main()或外部main()?
First thing, I think you meant "define", not "declare".
Second, there is no ruleas such, You can define wherever you want. It is all about the scopeof the definition.
If you define the structure inside
main(), the scope is limited tomain()only. Any other function cannot see that definition and hence, cannot make use of that structure definition.If you define the structure in a global scope, (i.e., outside
main()or any other function, for that matter), that definition is available globally and all the functions can seeand make use of the structure definition.
首先,我认为您的意思是“定义”,而不是“声明”。
其次,没有这样的规则,你可以在任何你想要的地方定义。这完全是关于定义的范围。
如果在 内部定义结构
main(),则范围仅限于main()。任何其他函数都无法看到该定义,因此无法使用该结构定义。如果您在全局范围内定义结构(即,外部
main()或任何其他函数,就此而言),该定义是全局可用的,并且所有函数都可以看到并使用结构定义。
回答by Some programmer dude
It have to do about scoping, when you define the structure inside the mainfunction then it's only defined in the scope of the mainfunction, so the billamtfunction can't know about it.
它与作用域有关,当您在main函数内部定义结构时,它仅在main函数范围内定义,因此billamt函数无法知道它。
回答by Kunal Saini
Structure are a like array only the main difference in Array can hold only same type of values but a structure can have different types of values so if you need to implement structure globally(by globally i mean it can be used in any other function too) define it outside the main and if you want to use your structure only in the main function define it inside it. Happy Coding :-)
结构是一个类似数组,只有 Array 的主要区别只能包含相同类型的值,但结构可以具有不同类型的值,因此如果您需要全局实现结构(全局我的意思是它也可以用于任何其他函数)在 main 之外定义它,如果您只想在 main 函数中使用您的结构,请在其中定义它。快乐编码:-)

