gpt4 book ai didi

c - 在函数中传递结构

转载 作者:行者123 更新时间:2023-11-30 20:08:09 25 4
gpt4 key购买 nike

以下代码在编译时出现错误:

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: warning: declaration of 'struct emp' will not be visible outside of this function [-Wvisibility]int add_employee(struct emp e)                        ^app.c:75:29: error: variable has incomplete type 'struct emp'int add_employee(struct emp e)                            ^app.c:75:25: note: forward declaration of 'struct emp'int add_employee(struct emp e)                        ^app.c:104:22: error: argument type 'struct emp' is incomplete    i = add_employee(emp);                     ^~~app.c:75:25: note: forward declaration of 'struct emp'int add_employee(struct emp e)                        ^1 warning and 2 errors generated.

最佳答案

查看main中的行

struct employees emp, *ptr;

看起来类型是struct员工

所以函数应该是

int add_employee(struct employees e)

关于c - 在函数中传递结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58212953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com