我在初始化结构体数组时遇到了问题。我不确定自己是否做得对,因为我收到了“从不兼容的指针类型进行初始化”和“从不兼容的指针类型进行赋值”的警告。我添加了代码,其中包含我收到这些警告的部分,当我尝试从结构体中打印数据时,我只会得到一些垃圾数据,例如@@###。
typedef struct
{
char* firstName;
char* lastName;
int day;
int month;
int year;
} student;
// Initialize array
student** students = malloc(sizeof(student));
int x;
for(x = 0; x < numStudents; x++)
{
// Here I get: "assignment from incompatible pointer type"
students[x] = (struct student*)malloc(sizeof(student));
}
int arrayIndex = 0;
// Add struct
// Create student struct
// Here I get: "initialization from incompatible pointer type"
student* newStudent = {"john", "smith", 1, 12, 1983};
// Add it to the array
students[arrayIndex] = newStudent;
arrayIndex++;