c - 在函数中传递结构
问题描述
以下代码在编译时出错:
int add_employee(struct emp e)
{
printf("%i\n", e.emp_no);
printf("Date of Birth:\n");
printf("%i / %i / %i\n", e.birth_date.dd, e.birth_date.mm, e.birth_date.yyyy);
printf("%s %s\n", e.first_name, e.last_name);
printf("%s\n", e.gender);
printf("Date of Hirting:\n");
printf("%i / %i / %i\n", e.hire_date.dd, e.hire_date.mm, e.hire_date.yyyy);
return 0;
}
int main()
{
struct employees emp, *ptr;
ptr = &emp;
int i=0;
ptr->emp_no = get_int("Employee No.: ");
printf("Date of Birth:\n");
ptr->birth_date.dd = get_int("Date: ");
ptr->birth_date.mm = get_int("Month: ");
ptr->birth_date.yyyy = get_int("Year: ");
ptr->first_name = get_string("First Name: ");
ptr->last_name = get_string("Last Name: ");
ptr->gender = get_string("Gender(M/F): ");
printf("Date of Hiring:\n");
ptr->hire_date.dd = get_int("Date: ");
ptr->hire_date.mm = get_int("Month: ");
ptr->hire_date.yyyy = get_int("Year: ");
i = add_employee(emp);
if(i == 0)
{
printf("Employee added successfully\n");
}
以下是我使用 clang 编译时的错误:
app.c:75:25:警告:“struct emp”的声明在此函数之外不可见 [-Wvisibility] int add_employee(struct emp e) ^ app.c:75:29:错误:变量的类型不完整 'struct emp' int add_employee(struct emp e) ^ app.c:75:25:注意: 'struct emp' 的前向声明 int add_employee(struct emp e) ^ app.c:104:22:错误:参数类型“struct emp”不完整 i = add_employee(emp); ^~~ app.c:75:25:注意: 'struct emp' 的前向声明 int add_employee(struct emp e) ^ 生成 1 个警告和 2 个错误。
解决方案
Looking at the line in main
struct employees emp, *ptr;
It seems the type is struct employees
.
So function should be
int add_employee(struct employees e)
推荐阅读
- python - 在 Scipy 的 cKDTree 例程中定义最小半径
- nancy - 如何在南希处理日期绑定?
- gradle - org.gradle.tooling.GradleConnectionException:无法获取“BuildEnvironment”类型的模型
- java - Android Studio 在打字时卡住了
- javascript - mongoDB 通过 find 得到“未定义”
- javascript - 转移焦点() - Angular 5
- php - 在多个数组中查找项目
- javascript - 带有函数和 this 的 Javascript 对象
- vue.js - 在 vuejs 中定义数据模式的正确模式
- docker - AWS ECS 为每个微服务运行实例