Передача структуры в функцию - PullRequest
0 голосов
/ 03 октября 2019

Следующий код выдает ошибки при компиляции:

int add_employee(struct emp e)
    76 {
    77     printf("%i\n", e.emp_no);
    78     printf("Date of Birth:\n");
    79     printf("%i / %i / %i\n", e.birth_date.dd, e.birth_date.mm, e.birth_date.yyyy);
    80     printf("%s %s\n", e.first_name, e.last_name);
    81     printf("%s\n", e.gender);
    82     printf("Date of Hirting:\n");
    83     printf("%i / %i / %i\n", e.hire_date.dd, e.hire_date.mm, e.hire_date.yyyy);
    84     return 0;
    85 }
    86 
    87 int main()
    88 {
    89     struct employees emp, *ptr;
    90     ptr = &emp;
    91     int i=0;
    92     ptr->emp_no = get_int("Employee No.: ");
    93     printf("Date of Birth:\n");
    94     ptr->birth_date.dd = get_int("Date: ");
    95     ptr->birth_date.mm = get_int("Month: ");
    96     ptr->birth_date.yyyy = get_int("Year: ");
    97     ptr->first_name = get_string("First Name: ");
    98     ptr->last_name = get_string("Last Name: ");
    99     ptr->gender = get_string("Gender(M/F): ");
   100     printf("Date of Hiring:\n");
   101     ptr->hire_date.dd = get_int("Date: ");
   102     ptr->hire_date.mm = get_int("Month: ");
   103     ptr->hire_date.yyyy = get_int("Year: ");
 <b>>></b>104     i = add_employee(emp);
   105     if(i == 0)
   106     {
   107         printf("Employee added successfully\n");
   108     }

Ниже приведена ошибка при компиляции с использованием clang:

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

1 Ответ

2 голосов
/ 03 октября 2019

Глядя на строку в main

struct employees emp, *ptr;

Кажется, тип: struct employees.

Так что функция должна быть

int add_employee(struct employees e)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...