I have the following code in C:
Two sturct:
typedef struct
{
int age;
int phone;
} Student;
typedef struct
{
int classNum;
Student student[1];
} ClassRoom;
And then I created memory for 10 students:
ClassRoom * classRoom = (ClassRoom*)malloc(sizeof(ClassRoom) + sizeof(Student) * 9);
Then I try to loop through all the students:
int i = 0;
for(i = 0; i < 10; i++)
{
classRoom->student[i].age = 20;
classRoom->student[i].phone = 20;
}
Then I got a lint warning:
Warning 662: Possible creation of out-of-bounds pointerby operator '[
Looks like it says this two lines has lint warnings:
classRoom->student[i].age = 20;
classRoom->student[i].phone = 20;
What is the problem and how to fix it? I can not change the struct of ClassRoom
, and are there any way to solve it?