传递给作为参数但未正常工作的结构

时间:2016-03-28 13:21:00

标签: c struct pass-by-value function-calls

所以我正在开发一个带有嵌套结构的项目,并将结构作为参数传递给函数。

这是我的主要功能:

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

问题是输入和存储数据的功能不起作用。每次程序运行时都需要输入,但在打印时会产生一些垃圾值。但是如果我在没有函数的情况下运行它(在main()函数下)它可以使用相同的代码。我似乎无法弄清楚代码中的问题,所以感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

C使用pass by值进行函数参数传递。所以,当你调用像

这样的函数时
take_input( a );

a的临时副本传递给该函数。无论您对被调用函数内的输入参数所做的任何更改,都不会对调用者中的实际a产生任何影响。

您需要传递结构变量的地址并对被调用函数内的变量进行更改。只有这样,所做的更改才会反射回到从调用函数传递的实际参数。

答案 1 :(得分:0)

将结构作为指针传递。在函数中使用箭头运算符。

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