arrays - 在 C 中传递结构数组并在不同的函数中打印信息
问题描述
我无法为一个程序获得正确的输出,其中我有一个包含员工详细信息的结构数组。我必须打印在主函数中输入的详细信息,在另一个函数 printEmployees 中使用原型“void printEmployees (employee emp[NUM_EMP], int c)”。有人可以让我知道出了什么问题吗?因为不是我输入的信息,而是打印出随机字符和符号。这是代码-
#include <stdio.h>
#include <stdlib.h>
#define SIZE 25
#define NUM_EMP 3
typedef struct Employees
{
char fname[20];
char lname[20];
int id;
char dependents [3][20];
}employee;
int main()
{
employee emp[3];
printf("Enter %d Employee details-\n", NUM_EMP);
int i;
for (i=0;i<NUM_EMP; i++)
{
printf("Enter %d employee first name-",i+1);
gets(emp[i].fname);
printf("Enter %d employee last name-", i+1);
gets(emp[i].lname);
printf("Enter %d employee id-",i+1);
scanf("%d", &emp[i].id);
printf("Enter %d Employee dependent names-\n", i+1);
int j;
for(j=0;j<=3;j++)
{
gets(emp[i].dependents);
}
}
int opt;
printf("What do you want to do? Choose a no. from 1 to 5-\n");
scanf("%d", &opt);
if (opt==1)
{
int a=0;
printEmployees (emp[NUM_EMP], a);
}
else
{
printf ("***"); //fill in other functions here
}
}
void printEmployees (employee emp[NUM_EMP], int c)
{
c=0;
for(c=0;c<NUM_EMP;c++)
{
printf("Employee First Name- %s", emp[c].fname);
printf("\nEmployee Last Name- %s", emp[c].lname);
printf("\nEmployee id- %d", emp[c].id);
int j;
for(j=0; j<=3; j++)
{
printf("\nDependent- %s", emp[c].dependents[j]);
}
}
}
解决方案
编译器会告诉你你做错了什么。它产生了很多警告。无论如何,您使用printEmployees
单个结构而不是结构数组进行调用。
printEmployees(emp[NUM_EMP], a)
扩展为:
printEmployees(emp[3], a)
什么时候应该调用它:
printEmployees(emp, a)
这是在没有警告的情况下修复的代码。如果您想学习一些东西,请不要复制此内容并收工。阅读编译器输出的警告并将其用作如何修复它们的指南。
#include <stdio.h>
#include <stdlib.h>
#define SIZE 25
#define NUM_EMP 3
typedef struct Employees {
char fname[20];
char lname[20];
int id;
char dependents[3][20];
} employee;
int main()
{
employee emp[3];
printf("Enter %d Employee details-\n", NUM_EMP);
for (int i = 0; i < NUM_EMP; i++) {
printf("Enter %d employee first name-", i + 1);
scanf("%s", emp[i].fname);
printf("Enter %d employee last name-", i + 1);
scanf("%s", emp[i].lname);
printf("Enter %d employee id-", i + 1);
scanf("%d", &emp[i].id);
printf("Enter %d Employee dependent names-\n", i + 1);
for (int j = 0; j < 3; j++) {
scanf("%s", emp[i].dependents[j]);
}
}
int opt;
printf("What do you want to do? Choose a no. from 1 to 5-\n");
scanf("%d", &opt);
if (opt == 1) {
int a = 0;
printEmployees(emp, a);
} else {
printf("***"); //fill in other functions here
}
}
void printEmployees (employee emp[NUM_EMP], int c)
{
c=0;
for(c=0;c<NUM_EMP;c++) {
printf("Employee First Name- %s", emp[c].fname);
printf("\nEmployee Last Name- %s", emp[c].lname);
printf("\nEmployee id- %d", emp[c].id);
for(int j = 0; j < 3; j++) {
printf("\nDependent- %s", emp[c].dependents[j]);
}
}
}
推荐阅读
- windows-10 - 如何在 git bas windows 10 上安装 automake、autoconf、libtool、pkg-config
- python - 在 tensorflows 功能 API 中以 (None,a,b) 形式引入张量
- python - 正则表达式匹配,直到找到除单词'and'之外的任何字符
- c# - 在 XSLT 中的字符串中转义“&”
- c++ - 在 Directshow 中将格式从更高分辨率更改为更低分辨率的问题,动态格式更改不会发生
- python - 我的 fig1.savefig 函数将我的图形保存为空白屏幕,我该如何解决?
- android - 通过蓝牙将图像从 ESP32 发送到 Android Studio 时出现“丢失字节”
- python - 如何使用 pandas 解析文本文件并创建列表
- algorithm - 动态规划问题:二维网格上的最大可访问区域
- docker - 从节点 alpine 和 mysql 构建的 Docker-compose 推送图像