I am trying to use malloc () and sizeof () to create a structure on a heap. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Employee
{
char first[21];
char last[21];
char title[21];
int salary;
};
struct Employee* createEmployee(char* first, char* last, char* title, int salary)
{
struct Employee* p = malloc(sizeof(struct Employee));
if (p != NULL)
{
strcpy(p->first, first);
strcpy(p->last, last);
strcpy(p->title, title);
p->salary, salary;
}
return p;
}
No my compiler (Visual C ++) tells me for the string struct Employee* p = malloc(sizeof(struct Employee));that the type "void *" cannot be converted to the type "Employee *". I do not know what is wrong here. The struct Employee seems to be invalid, but I don't understand why ...
source
share