当前位置: 代码迷 >> C语言 >> [求助]结构体变量作为函数参数
  详细解决方案

[求助]结构体变量作为函数参数

热度:346   发布时间:2007-11-06 22:04:50.0
[求助]结构体变量作为函数参数
#include<stdlib.h>
#include<stdio.h>
struct stud_type
{
char name[20];
long num;
int age;
char sex;
};
void main()
{
void list(struct stud_type student);
struct stud_type student[3],*p;
int i;
char numstr[20];
for(i=0,p=student;i<3;p++,i++)
{printf("Enter all data of student[%d]:\n",i);
scanf("%s%ld%d%c",p->name,&p->num,&p->age,&p->sex);
}
for(i=0;i<3;i++)
list(student[i]);
}
void list(student)
struct stud_type student;
{
printf("%-2s%8ld%6d%3c\n",student.name,student.num,student.age,student.sex);
}

这个程序在vc++编译时出现以下错误:C:\Documents and Settings\Administrator.F94E4AB1B271471\桌面\ckd.cpp(15) : warning C4101: 'numstr' : unreferenced local variable
C:\Documents and Settings\Administrator.F94E4AB1B271471\桌面\ckd.cpp(23) : error C2065: 'student' : undeclared identifier
C:\Documents and Settings\Administrator.F94E4AB1B271471\桌面\ckd.cpp(24) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
C:\Documents and Settings\Administrator.F94E4AB1B271471\桌面\ckd.cpp(24) : fatal error C1004: unexpected end of file found

该如何改正?
搜索更多相关的解决方案: 变量  结构体  函数  参数  

----------------解决方案--------------------------------------------------------
好象是定义有问题
----------------解决方案--------------------------------------------------------

帮你改了一下:
#include<stdlib.h>
#include<stdio.h>
struct stud_type
{
char name[20];
long num;
int age;
char sex;
};
int main(void)
{
void list(struct stud_type student);
struct stud_type student[3],*p;
int i;
char numstr[20];
for(i=0,p=student;i<3;p++,i++)
{printf("Enter all data of student[%d]:\n",i+1);
scanf("%s %ld %d %c",p->name,&p->num,&p->age,&p->sex);
// getchar();
}
for(i=0;i<3;i++)
list(student[i]);
return 0;
}
void list(struct stud_type student)
// struct stud_type student;
{
printf("%-2s%8ld%6d%3c\n",student.name,student.num,student.age,student.sex);
}


----------------解决方案--------------------------------------------------------

#include<stdlib.h>
#include<stdio.h>
typedef struct stud_type
{
char name[20];//name
long num;//num
int age;//age
char sex;//sex
} Stud_type;
int main(void)
{
void list(Stud_type student);
Stud_type student[3],*p;
int i;

for(i=0,p=student;i<3;p++,i++)
{printf("Enter all data of student[%d]:\n",i+1);
scanf("%s %ld %d %c",p->name,&p->num,&p->age,&p->sex);
getchar();
}
for(i=0;i<3;i++)
list(student[i]);
return 0;
}
void list(Stud_type student)
// struct stud_type student;
{
printf("%-2s%8ld%6d%3c\n",student.name,student.num,student.age,student.sex);
}
char numstr[20];没用啊




----------------解决方案--------------------------------------------------------
  相关解决方案