简体   繁体   中英

C programming initialize array of structures etc

I'm still relatively new to C and just had a 2 questions. I have a struct of students:

   struct student{
        Name   name;
        Comment  comment;

        int ID;
        float fee;
    }

There is a global array of 10000 students. Name and comment are structs that only hold pointers to chars. Also have 5 functions:

      void InitStudent();
      BOOL AddNewStudent();
      BOOL DeleteStudent();
      BOOL ReportStudents():
      BOOL Cleanup();
  1. In InitStudents(), all the student ID's must be initialized to -1. Not sure if theres an easier way to do it than loop through the entire array and initialize the ID's?

  2. In ReportStudents, we have to report all the students in the database that have been added. The problem is there is no StudentCount variable or something similar. The DeleteStudent function can also delete students leaving gaps in the array (can't change this). Also functions can't take any parameter. Is there any way to loop or print out all students without having to loop 10000 times checking if ID isn't -1?

thanks in advance

  1. Not really, if you do this through a function you'll need to loop through all of the student structures to set the ID to -1. Although, if you always check ID before using the structure you can get away with a memset since -1 has a convenient bit pattern.

     /* assuming all_students is your array of struct student (and not a pointer * to a block of memory) */ memset(all_students, -1, sizeof(all_students)); 
  2. Not without some other means of tracking which students are valid, through another data structure such as a linked list or a bitmap .

In InitStudents(), all the student ID's must be initialized to -1. Not sure if theres an easier way to do it than loop through the entire array and initialize the ID's?

Since the initializer expression (-1) is a constant expression, you could technically initialize a 10000-element array with the initializer list syntax, although I doubt that hammering out ten thousand structure initializers counts as "easier". A loop seems a viable solution.

In ReportStudents, we have to report all the students in the database that have been added. The problem is there is no StudentCount variable or something similar. The DeleteStudent function can also delete students leaving gaps in the array (can't change this). Also functions can't take any parameter. Is there any way to loop or print out all students without having to loop 10000 times checking if ID isn't -1?

Not really, if you have an array, then you have to loop through the entire array. 10000 elements is not many, however, it should not be an issue in terms of running time.

Looping through the array is the easiest way to do it.

On your second question it all depends on the entire implementation. You can sort you array and keep the next empty array position for your insertion. So when reporting you can just loop up to last index known to contain a valid student(Id is not -1)

Regarding retrival of a single student an index tree could help.

After the array had been populated the tree can be build carring the array index and the searchable values of each student.

Search operations are then performed using the tree, giving the array index as result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM