1

I'm trying to write a function in C that takes a struct array (of structs that hold employee information) as an argument and prints each member of each struct. For some reason my code isn't producing any output. Could anyone tell me where I am going wrong? Thanks.

Here it is:

#include <stdio.h>
#include <stdlib.h>

typedef struct Employee
{
    int number;
    char name[20];
    char department[15];
    double salary;
}Employee;

void employeePrint(Employee arr[]);

main()
{
    Employee e1 = {101,"John Smith\0","Accounting\0",54926.25};
    Employee e2 = {102,"Jane Q. Public\0","Retail\0",54926.24};
    Employee e3 = {103,"George Washington\0","Tech\0",70417.76};

    Employee empArr[3] = {e1,e2,e3};
    employeePrint(empArr);
}

void employeePrint(Employee arr[])
{
    int i;
    for(i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i)
    {
        printf("Number: %-4d\n",arr[i].number);
        printf("Name: %-6s\n",arr[i].name);
        printf("Department: %s\n",arr[i].department);
        printf("Salary: $%-2.2lf",arr[i].salary);
        printf("----------------");
    }
}
  • This is surely a duplicate of a number of other questions. It is basically about determining the size of an array passed to a function, which can't be done reliably with raw arrays (pointers). You have to pass the size explicitly, and probably separately (or use a structure type that defines an array type with the size included). – Jonathan Leffler Sep 28 '18 at 00:56
  • Ok well I searched for it and I didn’t know if it was different because the structs were in an array. Also, I didn’t if it was possible to not have a size argument for this function. I searched for about an hour to find this and I couldn’t, so I asked this question. This happens every time I ask a question on this site, and it’s really getting on my nerves. –  Sep 28 '18 at 17:55
  • Don't worry; finding duplicates is hard work, even for those who've been around for a decade. I have many common duplicates bookmarked; I don't have this one bookmarked. It didn't take me all that long to find it because I had a shrewd idea about the search term to use ('`[c] size array function`', and sort by votes, not relevance — the two are closely correlated, but not identical), but I did have to run a search, which is why there were 17 hours between noting that this was a duplicate and finding the actual duplicate. (There was also some sleep, and this thing called 'life', I believe.) – Jonathan Leffler Sep 28 '18 at 18:03
  • Note that if you wish, you can accept one of the answers, which will benefit both the person who answered and you; you'll get 2 points rep and the answerer 15. – Jonathan Leffler Sep 28 '18 at 18:04

2 Answers2

2

The problems are here: void employeePrint(Employee arr[]) and for(i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i). When you pass an array to a function like that, it decomposes to a pointer. Pointers don't store the size of the original array anymore, so sizeof(arr) is giving you the length of a pointer, which is smaller than your struct, so the division rounds to 0. To fix the problem, pass the size as another parameter, or use some sort of sentinel to know when to stop looping.

2

replace:

void employeePrint(Employee arr[])

with:

void employeePrint(size_t len, Employee arr[len])

then pass the length of the array in the function call, it should produce what you are looking for.

rmoro
  • 393
  • 1
  • 11