【发布时间】: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