回复 2楼 pbreak
/* function InsertStudent
------------------------------------ */
/*This function prompts the user to enter a new student name.
The precise form of the prompt is
Enter Student Name to be added to Class List:
Then this function reads the name from standard input.
But it must be able to read any name of any size!! Thus you have
to allocate memory (using malloc()) to read the input into, and if
it is not big enough, must use realloc() to extend it. You are not
allowed to allocate more than 10 bytes in one allocation or
extend your segment by more than 10 bytes in one call to realloc().
Do not forget to deallocate the memory before exiting the function!
Once the function has the name to be entered, it traverses the array student
to find out if the name is already there. If so, the function displays
a warning message "student xxx already in the class list" and terminates (
where xxx is the name of the student). If the name is not in the array, the
array students is extended by one item (using realloc()) and a copy
of the string with the name is "placed" there. Also the array marks is
extended by one row (using realloc()) and all five marks in that row are set to
-1. Then updated arrays students and marks are passed to SortClass()
so they can be sorted alphabetically, as you are required to keep the class
list and the list of marks in alphabetical order.
Note that both students and marks are passed to this function "by reference",
for this function on occasions must be able to modify the value stored in the
pointer student (in main()) - when putting there the very first student or when
realloc() possibly "moves" the array in the memory somewhere else. The same applies
to marks.*/
void InsertStudent(char*** students,int*** marks)
{
char* buf;
int found, i, j, size;
char** students1;
int** marks1;
students1 = *students;
marks1 = *marks;
char *name;
printf("Enter Student Name to be added to Class List:\n");
fflush(stdout);
name = ReadLine();
if (students1 == NULL)
{
if ((students1 = (char**) malloc(2*sizeof(char*))) == NULL)merror(2);
if ((students1[0] = (char*) malloc(strlen(name)+1)) == NULL)merror(3);
strcpy(students1[0],name);
students1[1] = NULL;
if ((marks1 = (int**) malloc(2*sizeof(int*))) == NULL)merror(4);
if ((marks1[0] = (int*) malloc(5*sizeof(int))) == NULL)merror(5);
marks1[0][0]=marks1[0][1]=marks1[0][2]=marks1[0][3]=marks1[0][4]=-1;
marks1[1] = NULL;
*students = students1;
*marks = marks1;
return;
}
/* do we have the student yet ? */
for(found = i = 0; students1[i] != NULL; i++)
{
if (strcmp(students1[i],name) == 0)
{
found = 1;
break;
}
}
if (found)
{
printf("student %s already in the class list\n",name);
return;
}
/* so it is not in the class list yet */
students1 = (char**) realloc((void*)students1,(i+2)*sizeof(char*));
if (students1 == NULL)
merror(6);
if ((students1[i] = (char*) malloc(strlen(name)+1)) == NULL)
merror(7);
strcpy(students1[i],name);
students1[i+1] = NULL;
free((void*) name);
if ((marks1 = (int**) realloc((void*)marks1,sizeof(int*)*(i+2))) == NULL)
merror(8);
if ((marks1[i] = (int*) malloc(5*sizeof(int))) == NULL)
merror(9);
marks1[i][0]=marks1[i][1]=marks1[i][2]=marks1[i][3]=marks1[i][4]=-1;
marks1[i+1] = NULL;
SortClass(students1,marks1);
*students = students1;
*marks = marks1;
}