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日
  • 展开全部

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效