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);
}

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

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632