I have a problem where I need to take a number of students, and then enter their names and a grade for each. I have it all working except for the malloc. I'm also using structs.
Code:
#define MaxNameLength 512
#define MinStudents 1
#define MaxStudents 10
struct Student
{
char* names;
float pGrades;
};
typedef struct Student Student;
int main( int argc, char** argv )
{
int numStudents = 0;
int i;
char longName[MaxNameLength];
int nameLength;
Student* students;
Student.names = malloc( numStudents * sizeof( Student.names* ) );
if( !Student.names )
{
printf( "Insufficient memory to allocate names array of length %u\n",
numStudents * sizeof( char* ) );
exit( 1 );
}
That is the first malloc for the students names.
Code:
Student.pGrades = malloc( numStudents * sizeof( Student.pGrades ) );
if( !Student.pGrades )
{
printf( "Insufficient memory to allocate names array of length %u\n",
numStudents * sizeof( float ) );
exit( 1 );
}
This is the malloc for their grades.
Any help would be appreciated, if this doesnt really make sense, I can post all of the code.