【问题标题】:Structure passed to function as parameter but not working properly结构作为参数传递给函数但不能正常工作
【发布时间】:2016-07-15 18:24:50
【问题描述】:

所以我正在研究一个具有嵌套结构并将结构作为参数传递给函数的项目。

这是我的主要功能:

#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() 函数下),它可以使用相同的代码正常工作。我似乎无法找出代码中的问题,因此不胜感激。

【问题讨论】:

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


    【解决方案1】:

    C 使用按值传递来传递函数参数。所以,当你像这样调用函数时

    take_input( a );
    

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

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

    【讨论】:

    • 按值传递大型结构也可能会严重影响性能。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-04
      • 2020-04-01
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多