学习C基础- Struct函数:



我目前正试图从struct函数创建一个程序,如下所示。我不想直接从程序中打印一行,而是想修改程序,让它打印一条消息,要求用户输入关于某个人的信息,并按照struct所指示的方式显示。

不要做"struct person "然后插入信息,我希望它以scanf的形式输入

#include <stdio.h>
typedef struct {
int day, month, year;
} DATA;
typedef struct person {
char name[100];
int age;
float salary;
DATA birth;
} PERSON;
void Mostrar(struct person x){
printf("name: %sn",x.name);
printf("age: %dn",x.age);
printf("Salário: %fn",x.salary);
printf("Dt.birth: %d/%d/%dn",x.birth.day, x.birth.month, x.birth.year);
}
int main(){
struct person p = {"Carlos", 23, 12345, {23,5,1954}};
Mostrar(p);
}

谢谢大家的帮助!

直接使用scanf

PERSON getPerson(void)
{
PERSON person;
if(!fgets(person.name, sizeof(person.name), stdin)) { /* error handling */} //you can also remove n at the end if you need to
if(scanf("%d %f", &person.age, &person.salary) != 2) { /* error handling */}
if(scanf("%d %d %d", &person.birth.day, &person.birth.month, &person.birth.year) != 3) { /* error handling */}
return person;
}

最新更新