This is incorrect:
student** students = malloc(sizeof(student));
You do not want a **
. You want a *
and enough space for how ever many students you need
student *students = malloc(numStudents * sizeof *students); // or sizeof (student)
for (x = 0; x < numStudents; x++)
{
students[x].firstName = "John"; /* or malloc and strcpy */
students[x].lastName = "Smith"; /* or malloc and strcpy */
students[x].day = 1;
students[x].month = 12;
students[x].year = 1983;
}
If you still want to use the code in your "//add struct" section, you'll need to change the line:
student* newStudent = {"john", "smith", 1, 12, 1983};
to
student newStudent = {"john", "smith", 1, 12, 1983};
You were getting "initialization from incompatible pointer type" because you were attempting to initialize a pointer to student
with an object of type student
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…