微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 c/c++ 中将动态结构数组传递给 fun

如何解决在 c/c++ 中将动态结构数组传递给 fun

我有这个作业https://i.stack.imgur.com/fel89.png 创建一个结构的动态数组,然后发送给两个函数一个要求用户输入值,然后作者打印这些值。使用 cc++。 我用 c 试过,但它显示错误,你能帮我吗? 谢谢。

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

struct Employee {
    int Id;
    char Name;
    int Degree;
    int Age;
}

void inputFun(struct Employee * EmpArr,int empNum);
void printFun(struct Employee * EmpArr,int empNum);

int main()
{
    int empNum;
    printf("Hello,Specify the Number of Employees Please");
    scanf("%d",&empNum);
    
    struct Employee * MyEmpArr = malloc(empNum*sizeof(struct Employee)); //array of structure
    
    inputFun(MyEmpArr,empNum);
    
    printf("\n");
    
    printFun(MyEmpArr,empNum);
    
    return 0;
}
void inputFun(struct Employee * EmpArr,int empNum){
   for(int i = 0; i < empNum; i++ ){
        printf("\nEnter details of Employee %d\n\n",i+1);

        printf("Id: ");
        scanf("%d",&EmpArr[i].Id);

        printf("Name: ");
        scanf("%c",&EmpArr[i].Name);

        printf("Degree: ");
        scanf("%d",&EmpArr[i].Degree);
        
        printf("Age: ");
        scanf("%d",&EmpArr[i].Age);
    }
}
void printFun(struct Employee * EmpArr,int empNum){
    printf("Employee details are: ");
    for(int i = 0; i < empNum; i++ )
        printf("%d\t%c\t%d\t%d\n",EmpArr[i].Id,EmpArr[i].Name,EmpArr[i].Degree,EmpArr[i].Age);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。