I am having a problem initializing an array of structs. I'm not sure if I am doing it right because I get "initialization from incompatible pointer type" & "assignment from incompatible pointer type". I added in the code where I get these warnings, and when I try to print the data from the struct I just get garbage such as @@###
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++;
When initializing, shoudn't it be like this?
However, why pointer to a pointer? Just with a pointer to struct is enough I think.
This is incorrect:
You do not want a
**
. You want a*
and enough space for how ever many students you needIf you still want to use the code in your "//add struct" section, you'll need to change the line:
to
You were getting "initialization from incompatible pointer type" because you were attempting to initialize a pointer to
student
with an object of typestudent
.Unrelated to the compiler warnings, but your initial malloc is wrong; you want:
To allocate room for a total of 'numStudents' pointers to a student. The line:
Should be:
There's no such thing as 'struct student'. You've declared an unnamed struct and typedef'd it to 'student'. Compare and contrast with:
Which would create a 'struct student' type but require you (in C) to explicitly refer to struct student rather than merely student elsewhere. This rule is changed for C++, so your compiler may be a bit fuzzy about it.
As for:
That should be:
As the curly brace syntax is a direct literal, not something somewhere else that you need to point to.
EDIT: on reflection, I think aaa may have taken more of an overview of this than I have. Is it possible that you're inadvertently using an extra level of pointer dereference everywhere? So you'd want:
And:
Subject the array not being used outside of scope of newStudent. Otherwise copying the pointers to strings is incorrect.