Abi
Abi

Reputation: 13

Passing an array of structs in C and printing the info in a different function

I'm having trouble in getting the correct output for a program where I have an array of struct with details of employees. I have to print the details entered in the main function, in another function printEmployees with the prototype "void printEmployees (employee emp[NUM_EMP], int c)". Could someone please let me know what's going wrong? Since instead of the information I've entered it's printing out random characters and symbols. Here's the code-

#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]);
        }

    }
}

Upvotes: 0

Views: 359

Answers (1)

Bastian Kalthoff
Bastian Kalthoff

Reputation: 36

The compiler tells you what you're doing wrong. It was generating a lot of warnings. Anyhow, you're calling printEmployees with a single struct, not a struct array.

printEmployees(emp[NUM_EMP], a)

which expands to:

printEmployees(emp[3], a)

when it should be called with:

printEmployees(emp, a)

Here's the code fixed without warnings. If you want to learn something, don't copy this and call it a day. Read the warnings of your compiler output and use this as a guide of how to fix them.

#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]);
        }
    }
}

Upvotes: 2

Related Questions