首页 > 解决方案 > 在 C 中传递结构数组并在不同的函数中打印信息

问题描述

我无法为一个程序获得正确的输出,其中我有一个包含员工详细信息的结构数组。我必须打印在主函数中输入的详细信息,在另一个函数 printEmployees 中使用原型“void printEmployees (employee emp[NUM_EMP], int c)”。有人可以让我知道出了什么问题吗?因为不是我输入的信息,而是打印出随机字符和符号。这是代码-

#include <stdio.h>
#include <stdlib.h>
#define SIZE 25
#define NUM_EMP 3
typedef struct Employees
{
 char fname[20];
 char lname[20];
 int id;
 char dependents [3][20];
}employee;
int main()
{
employee emp[3];
 printf("Enter %d Employee details-\n", NUM_EMP);
 int i;
 for (i=0;i<NUM_EMP; i++)
 {
     printf("Enter %d employee first name-",i+1);
     gets(emp[i].fname);
     printf("Enter %d employee last name-", i+1);
     gets(emp[i].lname);
     printf("Enter %d employee id-",i+1);
     scanf("%d", &emp[i].id);
     printf("Enter %d Employee dependent names-\n", i+1);
     int j;
     for(j=0;j<=3;j++)
     {
         gets(emp[i].dependents);
     }
 }
 int opt;
 printf("What do you want to do? Choose a no. from 1 to 5-\n");
 scanf("%d", &opt);
 if (opt==1)
 {
   int a=0;
   printEmployees (emp[NUM_EMP], a);

 }
 else
 {
     printf ("***"); //fill in other functions here
 }
}
void printEmployees (employee emp[NUM_EMP], int c)
{
    c=0;
    for(c=0;c<NUM_EMP;c++)
    {
        printf("Employee First Name- %s", emp[c].fname);
        printf("\nEmployee Last Name- %s", emp[c].lname);
        printf("\nEmployee id- %d", emp[c].id);
        int j;
        for(j=0; j<=3; j++)
        {
         printf("\nDependent- %s", emp[c].dependents[j]);
        }

    }
}

标签: arrayscfunctionstruct

解决方案


编译器会告诉你你做错了什么。它产生了很多警告。无论如何,您使用printEmployees单个结构而不是结构数组进行调用。

printEmployees(emp[NUM_EMP], a)

扩展为:

printEmployees(emp[3], a)

什么时候应该调用它:

printEmployees(emp, a)

这是在没有警告的情况下修复的代码。如果您想学习一些东西,请不要复制此内容并收工。阅读编译器输出的警告并将其用作如何修复它们的指南。

#include <stdio.h>
#include <stdlib.h>
#define SIZE 25
#define NUM_EMP 3
typedef struct Employees {
    char fname[20];
    char lname[20];
    int id;
    char dependents[3][20];
} employee;

int main()
{
    employee emp[3];
    printf("Enter %d Employee details-\n", NUM_EMP);
    for (int i = 0; i < NUM_EMP; i++) {
        printf("Enter %d employee first name-", i + 1);
        scanf("%s", emp[i].fname);
        printf("Enter %d employee last name-", i + 1);
        scanf("%s", emp[i].lname);
        printf("Enter %d employee id-", i + 1);
        scanf("%d", &emp[i].id);
        printf("Enter %d Employee dependent names-\n", i + 1);

        for (int j = 0; j < 3; j++) {
            scanf("%s", emp[i].dependents[j]);
        }
    }
    int opt;
    printf("What do you want to do? Choose a no. from 1 to 5-\n");
    scanf("%d", &opt);
    if (opt == 1) {
        int a = 0;
        printEmployees(emp, a);
    } else {
        printf("***"); //fill in other functions here
    }
}

void printEmployees (employee emp[NUM_EMP], int c)
{
    c=0;
    for(c=0;c<NUM_EMP;c++) {
        printf("Employee First Name- %s", emp[c].fname);
        printf("\nEmployee Last Name- %s", emp[c].lname);
        printf("\nEmployee id- %d", emp[c].id);
        for(int j = 0; j < 3; j++) {
         printf("\nDependent- %s", emp[c].dependents[j]);
        }
    }
}

推荐阅读