Structures help

In this program, I just want to input an employee's data and then have it show me the report. My question is what am I doing wrong here because I'm not getting the right report. I think that I have to do something with pointers to a structure but I'm confused with the -> operator, the . operator, and the * operator.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <string.h>


typedef struct EmpInfo
{
    char firstname[10+1],lastname[15+1],fullname[25+1];
    float payrate, hours, defr;
}EmpRecord;

void InputEmpData(EmpInfo theEmps);
void DispalyData(EmpInfo theEmps);
int main(void)
{
    EmpInfo theEmps;
    InputEmpData(theEmps);
    DispalyData(theEmps);
    fflush(stdin);
    getchar();
    return 0;
}

void InputEmpData(EmpInfo theEmps)
{
	printf("Enter employee's first name: ");
	scanf("%s",theEmps.firstname);
	printf("Enter employee's last name: ");
	scanf("%s",theEmps.lastname);
	printf("Enter hourly payrate: ");
	scanf("%f",&theEmps.payrate);
	printf("Enter hours worked this pay peroid: ");
	scanf("%f",&theEmps.hours);
	printf("Enter deferred amount:");
	scanf("%f",&theEmps.defr);
	strcpy(theEmps.fullname,theEmps.lastname);
	strcat(theEmps.fullname,", ");
	strcat(theEmps.fullname,theEmps.firstname);
}
void DispalyData(EmpInfo theEmps)
{
     printf("%c\n Name: ",theEmps.fullname);
     printf("%f\n Payrate:",theEmps.payrate);
     printf("%f\n Hours: ",theEmps.hours);
     printf("%f\n Deferred: ",theEmps.defr);      
}

Last edited on
Your input data function doesn't actually do anything of any use because it operates on a copy that is thrown away when the function ends. Try passing its parameter by reference.
That's what I don't understand about structures, do I pass the members of the struct by reference or do I pass the struct variable by reference?
I think that I have to do something with pointers to a structure but I'm confused with the -> operator, the . operator, and the * operator.
If p is a pointer you write *p to get what p points to. If p is a pointer to a struct object you you can access the members of that object using the dot operator like normal (*p).member. As a shorthand for this syntax you can use the -> operator p->member. Both way means the same.

That's what I don't understand about structures, do I pass the members of the struct by reference or do I pass the struct variable by reference?
Both are possible but it probably makes more sense to pass the struct object.
Thanks for directing me to the right direction, I've fixed it.
Now, I'm having trouble again with this code. This time, I tried to use an array of structures but it keeps saying that there is a linker error. Can you please point out my error to me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <string.h>


typedef struct EmpInfo
{
    char firstname[10+1],lastname[15+1],fullname[25+1];
    float payrate,hours, defr;
}EmpInfo;

void InputEmpData(EmpInfo theEmps[3]);
void DispalyData(EmpInfo theEmps[3]);
int main(void)
{
    EmpInfo theEmps[3];
    InputEmpData(&theEmps[3]);
    DispalyData(&theEmps[3]);
    fflush(stdin);
    getchar();
    return 0;
}

void InputEmpData(EmpInfo *theEmps[3])
{
    for (int count = 0;count<3;count++)
    {
	printf("Enter employee's first name: ");
	scanf("%s",theEmps[count]->firstname);
	printf("Enter employee's last name: ");
	scanf("%s",theEmps[count]->lastname);
	printf("Enter hourly payrate: ");
	scanf("%f",&theEmps[count]->payrate);
	printf("Enter hours worked this pay peroid: ");
	scanf("%f",&theEmps[count]->hours);
	printf("Enter deferred amount:");
	scanf("%f",&theEmps[count]->defr);
	strcpy(theEmps[count]->fullname,theEmps[count]->lastname);
	strcat(theEmps[count]->fullname,", ");
	strcat(theEmps[count]->fullname,theEmps[count]->firstname);
    }
}
void DispalyData(EmpInfo theEmps[3])
{
         for(int count = 0;count<3;count++)
    {
     printf(" Name: %s\n",theEmps[count].fullname);
     printf(" Payrate: %f\n",theEmps[count].payrate);
     printf(" Hours: %f\n",theEmps[count].hours);
     printf(" Deferred: %f\n",theEmps[count].defr);      
}}
You have declared InputEmpData as
void InputEmpData(EmpInfo theEmps[3]);
but when you define it you have added a * which makes the parameter a different type and the function a different function.
void InputEmpData(EmpInfo *theEmps[3])
I've just removed the * from
void InputEmpData(EmpInfo *theEmps[3])
and now I'm getting errors from my -> operators saying
base operand of `->' has non-pointer type `EmpInfo'
Okay, I've just fixed it. I just changed all the operators to . operator but now I'm getting semantic errors.
Topic archived. No new replies allowed.