jlHe233 2022-04-21 00:51 采纳率: 88.6%
浏览 19
已结题

pointer of struct出现问题

19行显示error: assignment to employee* from incompatiple pointer type int*


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

struct Employee{
    char Name[100];
    char Position[100];
    int Salary;
};typedef struct Employee employee;
void setEmployee(char n[], char p[] , int sal, employee *e);
void showInfo(employee e);

int main()
{
    int numOfEmployee;
    scanf("%d",&numOfEmployee);
    employee *e;
    e = (int*) malloc ( numOfEmployee * sizeof(int)); //显示error:assignment to employee* from incompatiple pointer type int*
    char n[100], p[100];
    int sal;
     for (int i = 0 ; i < numOfEmployee ; i++)
    {
         scanf("%s%s%d",n,p,&sal);
         setEmployee( n,p,sal,&e[i]);
     }
     for (int i = 0 ; i <numOfEmployee ; i++)
    {
         showInfo(e[i]);
     } 
     free(e);
 }

void setEmployee(char n[], char p[] , int sal, employee *e)
{
    strcpy(e->Name , p);
    e->Salary=sal;
    
    
}
void showInfo(employee e){
    printf("Name: %s\n", e.Name);
}
  • 写回答

2条回答 默认 最新

  • 关注

    e 是 employee * 类型

    e = (int*) malloc ( numOfEmployee * sizeof(int)); 中int改成employee
    

    改成

    e = (employee*) malloc ( numOfEmployee * sizeof(employee));
    

    你题目的解答代码如下:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct Employee{
        char Name[100];
        char Position[100];
        int Salary;
    };typedef struct Employee employee;
    void setEmployee(char n[], char p[] , int sal, employee *e);
    void showInfo(employee e);
    
    int main()
    {
        int numOfEmployee;
        scanf("%d",&numOfEmployee);
        employee *e;
        e = (employee*) malloc ( numOfEmployee * sizeof(employee)); //int改成employee
        char n[100], p[100];
        int sal;
         for (int i = 0 ; i < numOfEmployee ; i++)
        {
             scanf("%s%s%d",n,p,&sal);
             setEmployee( n,p,sal,&e[i]);
         }
         for (int i = 0 ; i <numOfEmployee ; i++)
        {
             showInfo(e[i]);
         }
         free(e);
     }
    
    void setEmployee(char n[], char p[] , int sal, employee *e)
    {
        strcpy(e->Name , p);
        e->Salary=sal;
    
    
    }
    void showInfo(employee e){
        printf("Name: %s\n", e.Name);
    }
    

    img

    如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月29日
  • 已采纳回答 4月21日
  • 修改了问题 4月21日
  • 修改了问题 4月21日
  • 展开全部

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助