【发布时间】:2017-09-07 06:47:41
【问题描述】:
所以我在编译这个程序时遇到了问题,我就是不能让它工作,我的意思是如果我把inputstudent() 代码放在main() 里面,会容易得多,但我必须把在函数inputstudent() 中编写代码并从main() 调用。我知道这听起来很简单,但我无法理解。
#include <stdio.h>
#include <stdlib.h>
struct student
{
char surname[50];
int age;
char oname[50];
char address[50];
};
void displaystudent();
void inputstudent();
int main(){
struct student s;
inputstudent(s);
displaystudent(s);
return 0;
}
void inputstudent(struct student s){
printf("Enter the surname: ");
scanf("%s",s.surname);
printf("Enter the other name: ");
scanf("%s",s.oname);
printf("Enter the age: ");
scanf("%d",&s.age);
printf("Enter the address: ");
scanf("%s",s.address);
}
void displaystudent(struct student s)
{
printf("Surname: %s \n",s.surname);
printf("Oname: %s \n",s.oname);
printf("Age: %d \n",s.age);
printf("Address: %s",s.address);
}
【问题讨论】:
-
请正确格式化您的代码
-
"can't get it to work" 是什么意思?有什么问题?
-
void inputstudent(struct student s)->void inputstudent(struct student *s)和scanf("%s",s.surname);->scanf("%s",s->surname);等 -
您可能想尝试这里提出的方法:How to debug small programs
-
使用像
inputstudent(&s);这样的指针