user-0 提问: 关于结构体定义在main函数里头的问题，短短的程序而已

回复:
user: user-0
content: 引用 14 楼 wang0635 的回复:哪个老师教的？ 自学的~

user: user-0
content: 引用 13 楼 zhao4zhong1 的回复:[quote=引用 11 楼 nsxzclx 的回复:] [quote=引用 10 楼 zhao4zhong1 的回复:] #include<stdio.h> #define FORMAT  "%d\n%s\n%f\n%f\n%f\n" struct student {     int num;     char name[20];     float score[3]; }; void print(struct student *p) {     printf(FORMAT,p->num,p->name,p->score[0],p->score[1],p->score[2]); } void main() {     struct student stu={302,"Longxin",90,91,92};     print(&stu);     system("pause"); }   是的 放在main函数前边就可以了 是怎么回事呢？[/quote] 是的，把鼻子放在嘴上面就正常了 是怎么回事呢？  Summary of Declarations declaration :  declaration-specifiers attribute-seq opt init-declarator-list opt ;                         /* attribute-seq is Microsoft Specific */  declaration-specifiers :  storage-class-specifier declaration-specifiers opt type-specifier declaration-specifiers opt type-qualifier declaration-specifiers opt  attribute-seq :            /* attribute-seq is Microsoft Specific */  attribute attribute-seq opt  attribute : one of      /* Microsoft Specific */  __asm __fastcall  __based __inline  __cdecl __stdcall    init-declarator-list :  init-declarator init-declarator-list , init-declarator  init-declarator :  declarator declarator = initializer /* For scalar initialization */  storage-class-specifier :  auto register static extern typedef __declspec ( extended-decl-modifier-seq ) /* Microsoft Specific */  type-specifier :  void char short int __int8       /* Microsoft Specific */ __int16      /* Microsoft Specific */ __int32      /* Microsoft Specific */ __int64      /* Microsoft Specific */ long float double signed unsigned struct-or-union-specifier enum-specifier typedef-name  type-qualifier :  const volatile  declarator :  pointer opt direct-declarator  direct-declarator :  identifier ( declarator ) direct-declarator [ constant-expression opt] direct-declarator ( parameter-type-list )   /* New-style declarator */ direct-declarator ( identifier-list opt )      /* Obsolete-style declarator */  pointer :  * type-qualifier-list opt * type-qualifier-list opt pointer  parameter-type-list :                           /* The parameter list */  parameter-list parameter-list , ...  parameter-list :  parameter-declaration parameter-list , parameter-declaration  type-qualifier-list :  type-qualifier type-qualifier-list type-qualifier  enum-specifier :  enum identifier opt { enumerator-list } enum identifier  enumerator-list :  enumerator enumerator-list , enumerator  enumerator :  enumeration-constant enumeration-constant = constant-expression  enumeration-constant :  identifier  struct-or-union-specifier :  struct-or-union identifier opt { struct-declaration-list } struct-or-union identifier  struct-or-union :  struct union  struct-declaration-list :  struct-declaration struct-declaration-list struct-declaration  struct-declaration :  specifier-qualifier-list struct-declarator-list ;  specifier-qualifier-list :  type-specifier specifier-qualifier-list opt type-qualifier specifier-qualifier-list opt  struct-declarator-list :  struct-declarator struct-declarator-list , struct-declarator  struct-declarator :  declarator type-specifier declarator opt : constant-expression  parameter-declaration :  declaration-specifiers declarator                /* Named declarator */ declaration-specifiers abstract-declarator opt /* Anonymous declarator */  identifier-list :      /* For old-style declarator */  identifier identifier-list , identifier  abstract-declarator :         /* Used with anonymous declarators */  pointer pointer opt direct-abstract-declarator  direct-abstract-declarator :  ( abstract-declarator ) direct-abstract-declarator opt [ constant-expression opt ] direct-abstract-declarator opt ( parameter-type-list opt )  initializer :  assignment-expression { initializer-list }   /* For aggregate initialization */  { initializer-list , }  initializer-list :  initializer initializer-list , initializer  type-name :  specifier-qualifier-list abstract-declarator opt  typedef-name :  identifier  extended-decl-modifier-seq :/*   Microsoft Specific */  extended-decl-modifier opt extended-decl-modifier-seq extended-decl-modifier  extended-decl-modifier :   /* Microsoft Specific */  thread naked dllimport dllexport [/quote] 这个……谢谢你的幽默

user: user-1
content: 哪个老师教的？

user: user-2
content: 引用 11 楼 nsxzclx 的回复:[quote=引用 10 楼 zhao4zhong1 的回复:] #include<stdio.h> #define FORMAT  "%d\n%s\n%f\n%f\n%f\n" struct student {     int num;     char name[20];     float score[3]; }; void print(struct student *p) {     printf(FORMAT,p->num,p->name,p->score[0],p->score[1],p->score[2]); } void main() {     struct student stu={302,"Longxin",90,91,92};     print(&stu);     system("pause"); }   是的 放在main函数前边就可以了 是怎么回事呢？[/quote] 是的，把鼻子放在嘴上面就正常了 是怎么回事呢？  Summary of Declarations declaration :  declaration-specifiers attribute-seq opt init-declarator-list opt ;                         /* attribute-seq is Microsoft Specific */  declaration-specifiers :  storage-class-specifier declaration-specifiers opt type-specifier declaration-specifiers opt type-qualifier declaration-specifiers opt  attribute-seq :            /* attribute-seq is Microsoft Specific */  attribute attribute-seq opt  attribute : one of      /* Microsoft Specific */  __asm __fastcall  __based __inline  __cdecl __stdcall    init-declarator-list :  init-declarator init-declarator-list , init-declarator  init-declarator :  declarator declarator = initializer /* For scalar initialization */  storage-class-specifier :  auto register static extern typedef __declspec ( extended-decl-modifier-seq ) /* Microsoft Specific */  type-specifier :  void char short int __int8       /* Microsoft Specific */ __int16      /* Microsoft Specific */ __int32      /* Microsoft Specific */ __int64      /* Microsoft Specific */ long float double signed unsigned struct-or-union-specifier enum-specifier typedef-name  type-qualifier :  const volatile  declarator :  pointer opt direct-declarator  direct-declarator :  identifier ( declarator ) direct-declarator [ constant-expression opt] direct-declarator ( parameter-type-list )   /* New-style declarator */ direct-declarator ( identifier-list opt )      /* Obsolete-style declarator */  pointer :  * type-qualifier-list opt * type-qualifier-list opt pointer  parameter-type-list :                           /* The parameter list */  parameter-list parameter-list , ...  parameter-list :  parameter-declaration parameter-list , parameter-declaration  type-qualifier-list :  type-qualifier type-qualifier-list type-qualifier  enum-specifier :  enum identifier opt { enumerator-list } enum identifier  enumerator-list :  enumerator enumerator-list , enumerator  enumerator :  enumeration-constant enumeration-constant = constant-expression  enumeration-constant :  identifier  struct-or-union-specifier :  struct-or-union identifier opt { struct-declaration-list } struct-or-union identifier  struct-or-union :  struct union  struct-declaration-list :  struct-declaration struct-declaration-list struct-declaration  struct-declaration :  specifier-qualifier-list struct-declarator-list ;  specifier-qualifier-list :  type-specifier specifier-qualifier-list opt type-qualifier specifier-qualifier-list opt  struct-declarator-list :  struct-declarator struct-declarator-list , struct-declarator  struct-declarator :  declarator type-specifier declarator opt : constant-expression  parameter-declaration :  declaration-specifiers declarator                /* Named declarator */ declaration-specifiers abstract-declarator opt /* Anonymous declarator */  identifier-list :      /* For old-style declarator */  identifier identifier-list , identifier  abstract-declarator :         /* Used with anonymous declarators */  pointer pointer opt direct-abstract-declarator  direct-abstract-declarator :  ( abstract-declarator ) direct-abstract-declarator opt [ constant-expression opt ] direct-abstract-declarator opt ( parameter-type-list opt )  initializer :  assignment-expression { initializer-list }   /* For aggregate initialization */  { initializer-list , }  initializer-list :  initializer initializer-list , initializer  type-name :  specifier-qualifier-list abstract-declarator opt  typedef-name :  identifier  extended-decl-modifier-seq :/*   Microsoft Specific */  extended-decl-modifier opt extended-decl-modifier-seq extended-decl-modifier  extended-decl-modifier :   /* Microsoft Specific */  thread naked dllimport dllexport

user: user-2
content: #include<stdio.h> #define FORMAT  "%d\n%s\n%f\n%f\n%f\n" struct student {     int num;     char name[20];     float score[3]; }; void print(struct student *p) {     printf(FORMAT,p->num,p->name,p->score[0],p->score[1],p->score[2]); } void main() {     struct student stu={302,"Longxin",90,91,92};     print(&stu);     system("pause"); }

user: user-3
content: 引用 10 楼 zhao4zhong1 的回复:#include<stdio.h> #define FORMAT  "%d\n%s\n%f\n%f\n%f\n" struct student {     int num;     char name[20];     float score[3]; }; void print(struct student *p) {     printf(FORMAT,p->num,p->name,p->score[0],p->score[1],p->score[2]); } void main() {     struct student stu={302,"Longxin",90,91,92};     print(&stu);     system("pause"); }  看到就直接+1

user: user-0
content: 引用 10 楼 zhao4zhong1 的回复:#include<stdio.h> #define FORMAT  "%d\n%s\n%f\n%f\n%f\n" struct student {     int num;     char name[20];     float score[3]; }; void print(struct student *p) {     printf(FORMAT,p->num,p->name,p->score[0],p->score[1],p->score[2]); } void main() {     struct student stu={302,"Longxin",90,91,92};     print(&stu);     system("pause"); }   是的 放在main函数前边就可以了 是怎么回事呢？

user: user-0
content: 引用 1 楼 plottowan_1977 的回复:void print(struct student *p); 函数声明在struct student声明之前就已经使用了该结构，把这句话放到结构声明后面去 void main() {         struct student     {         int num;         char name[20];         float score[3];     };     void print(struct student *p);      struct student stu={302,"Longxin",90,91,92};     print(&stu);     system("pause"); } 把他放到结构声明后面，void print(struct student *p);又有问题了。

user: user-0
content: 引用 6 楼 derekrose 的回复:有什么问题是很重要的 什么意思？

user: user-0
content: 引用 5 楼 plottowan_1977 的回复:[quote=引用 4 楼 nsxzclx 的回复:] 会不会是因为在后面print的函数定义中也用到这个结构体，如果定义在main函数中，就不能被他用到了？  应该是的，放最外面吧[/quote]  一般都是放在外面的是吗？感觉放在里边好多问题啊

user: user-4
content: 有什么问题是很重要的

user: user-5
content: 引用 4 楼 nsxzclx 的回复:会不会是因为在后面print的函数定义中也用到这个结构体，如果定义在main函数中，就不能被他用到了？  应该是的，放最外面吧

user: user-0
content: 会不会是因为在后面print的函数定义中也用到这个结构体，如果定义在main函数中，就不能被他用到了？

user: user-0
content: 把他放到结构声明后面，void print(struct student *p);又有问题了。

user: user-6
content: #include<stdio.h> #define FORMAT  "%d\n%s\n%f\n%f\n%f\n"  void main() {	  	struct student 	{ 		int num; 		char name[20]; 		float score[3]; 	}; 	struct student stu={302,"Longxin",90,91,92}; 	void print(struct student *p);  	print(&stu); 	system("pause"); }  void print(struct student *p) { 	printf(FORMAT,p->num,p->name,p->score[0],p->score[1],p->score[2]); }

user: user-5
content: void print(struct student *p); 函数声明在struct student声明之前就已经使用了该结构，把这句话放到结构声明后面去 void main() {         struct student     {         int num;         char name[20];         float score[3];     };     void print(struct student *p);      struct student stu={302,"Longxin",90,91,92};     print(&stu);     system("pause"); }