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);
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!