[英]How to pass a struct variable as an argument in c
I am trying to pass a struct variable as an argument into a function.我正在尝试将结构变量作为参数传递给 function。 But I keep getting errors At the function call Display_Info(person);
但我在 function 调用 Display_Info (person);时不断出错.
.
I searched for similar problems, and the solution I got didn't still work, I can't really see why this code is not compiling.我搜索了类似的问题,我得到的解决方案仍然不起作用,我真的不明白为什么这段代码没有编译。 One of the solution I tried was from https://fresh2refresh.com/c-programming/c-passing-struct-to-function/ and as you can see I have done just that and it didn't still compile.
我尝试的解决方案之一来自https://fresh2refresh.com/c-programming/c-passing-struct-to-function/ ,正如您所看到的,我已经做到了,但它仍然没有编译。 Pls I need assistance.
请问我需要帮助。
#include <stdio.h>
struct HealthProfile
{
char first_name[15];
char last_name[15];
char gender[15];
int date_of_birth[3];
float height;
float weight;
};
typedef struct HealthProfile Person;
Person Set_Data(); //prototype to get information
void Display_Info(Person person); //prototype to display personal information
int Get_Age(int year); //prototype to get age
void Heart_Rate(int age); //protorype to calculate heart rate
void BMI(float ht, float wt); //prototype to get Body Mass Index
int main()
{
Person person; //create object
person = Set_Data(); //assign data to person object
Display_Info(person);
Heart_Rate(Get_Age(person.date_of_birth[2]));
BMI(person.height, person.weight);
return 0;
}
Person Set_Data() //Function to recieve data
{
Person data;
printf("\n\n*****ENTER INFO*****\n");
printf("First Name: ");
scanf("%s", data.first_name);
printf("Last Name: ");
scanf("%s", data.last_name);
printf("Gender: ");
scanf("%s", data.gender);
printf("Month of birth(in num): ");
scanf("%d", &data.date_of_birth[0]);
printf("Day of birth(in num): ");
scanf("%d", &data.date_of_birth[1]);
printf("Year of birth(in num): ");
scanf("%d", &data.date_of_birth[2]);
printf("Height(in meters): ");
scanf("%f", &data.height);
printf("Weight(in kilograms): ");
scanf("%f", &data.weight);
return data;
}
void Display_Info(Person person)
{
printf("\n\n*****PERSONAL INFO*****\n");
printf("First name: %s", person.first_name);
printf("Last name: %s", person.last_name);
printf("Gender: %s", person.gender);
printf("Date of birth: %d/ %d/ %d",
person.date_of_birth[0],
person.date_of_birth[1],
person.date_of_birth[2]);
printf("Age: %d", Get_Age(person.date_of_birth[2]));
printf("Height: %.1f", person.height);
printf("Weight: %.1f", person.weight);
}
My compiler does not even have an error message for this, it just won't compile.我的编译器甚至没有错误消息,它只是无法编译。 But if I comment out the function call Display_Info(person);
但是如果我注释掉 function 调用 Display_Info (person); the code compiles.
代码编译。
You just pass it in specifying the struct as the argument type:您只需将其传递给指定结构作为参数类型:
void functionWithStructInput(Person inputPerson) {
//...
}
Also you made a typo, you defined the function as Displsy_Info
and tried to call it as Display_Info
.您还打错了字,您将 function 定义为
Displsy_Info
并尝试将其称为Display_Info
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.