Masquerade007
Masquerade007

Reputation: 71

Structure passed to function as parameter but not working properly

So I was working on a project with nested structures and passing structures to functions as arguments.

Here is my main function:

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

struct date {
    int dd;
    int mm;
    int yy;
};

struct employee {
    char fn[50];
    char ln[50];
    struct date dob;
    struct date sd;
    int salary;
};

void take_input( struct employee e );
void give_output( struct employee e );

int main(void)
{
    struct employee a; struct employee b; struct employee c; struct employee d; struct employee f;

    take_input( a );
    take_input( b );
    take_input( c );
    take_input( d );
    take_input( f );

    give_output( a );
    give_output( b );
    give_output( c );
    give_output( d );
    give_output( f );

    return 0;
}

And here are the two functions:

void take_input( struct employee e )
{
    printf("\nFirst name: ");
    gets(e.fn);
    printf("\nLast name: ");
    gets(e.ln);
    printf("\nDate of Birth: ");
    scanf("%d %d %d", &e.dob.dd, &e.dob.mm, &e.dob.yy);
    printf("\nDate of Joining: ");
    scanf("%d %d %d", &e.sd.dd, &e.sd.mm, &e.sd.yy);
    printf("\nSalary: ");
    scanf("%d", &e.salary);
}

void give_output( struct employee e )
{
    printf("%s", e.fn);
    printf(" %s", e.ln);
    printf("\nDate of Birth: %d/%d/%d", e.dob.dd, e.dob.mm, e.dob.yy);
    printf("\nStarting Date: %d/%d/%d", e.sd.dd, e.sd.mm, e.sd.yy);
    printf("\nSalary: $%d\n", e.salary);
}

The problem is the function for taking input and storing data is not working. Every time the program runs it takes input but while printing it gives some garbage value. But if I run it without functions(under the main() function) it works fine with the same codes. I can't seem to figure out the problem in the code so any help is appreciated.

Upvotes: 2

Views: 585

Answers (2)

Albert
Albert

Reputation: 76

Pass the structure as pointer. Use the arrow operator inside your functions.

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

struct date {
    int dd;
    int mm;
    int yy;
};

struct employee {
    char fn[50];
    char ln[50];
    struct date dob;
    struct date sd;
    int salary;
};

void take_input( struct employee e );
void give_output( struct employee e );

int main(void)
{
    struct employee a; struct employee b; struct employee c; struct employee d; struct employee f;

    take_input( &a );
    take_input( &b );
    take_input( &c );
    take_input( &d );
    take_input( &f );

    give_output( &a );
    give_output( &b );
    give_output( &c );
    give_output( &d );
    give_output( &f );

    return 0;
}

void take_input( struct employee *e )
{
    printf("\nFirst name: ");
    gets(e->fn);
    printf("\nLast name: ");
    gets(e->ln);
    printf("\nDate of Birth: ");
    scanf("%d %d %d", e->dob->dd, e->dob->mm, e->dob->yy);
    printf("\nDate of Joining: ");
    scanf("%d %d %d", e->sd->dd, e->sd->mm, e->sd->yy);
    printf("\nSalary: ");
    scanf("%d", e->salary);
}

void give_output( struct employee *e )
{
    printf("%s", e->fn);
    printf(" %s", e->ln);
    printf("\nDate of Birth: %d/%d/%d", e->dob->dd, e->dob->mm, e->dob->yy);
    printf("\nStarting Date: %d/%d/%d", e->sd->dd, e->sd->mm, e->sd->yy);
    printf("\nSalary: $%d\n", e->salary);
}

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

C uses pass by value for function argument passing. so, when you call the function like

take_input( a );

a temporary copy of a is passed to the function. Whatever changes you make to the input parameter inside the called function, will not have any effect on the actual a present in the caller.

You need to pass the address of the structure variable and make changes to that inside the called function. Only then, the changes made will be reflected back to the actual argument passed from the caller function.

Upvotes: 3

Related Questions