0

My assignment requires the use of the following linked list structure:

      struct studentNode {
        int     id;
        char    *fname;
        char    *lname;
        int     programs[x];
        int     labs[x];
        int     exams[x];
        int     percent;
        double  grade;

        struct  studentNode *next;
      };

My problem is, the arrays for programs, labs and exams are being loaded from a file and are to be variable lengths.

I tried using a pointer to the array, however whenever I updated the array for a new student it would replace the scores for everyone in the linked list.

I've tried going over this with the instructor and he tells me to google it. :(

So far, I haven't had any luck and it's beyond the scope of what our book covers.

Any help would be appreciated.

4
  • Show us some code you've already written. Commented Mar 6, 2011 at 19:10
  • Was having trouble with the display, it's up now. Commented Mar 6, 2011 at 19:11
  • When you say whenever I updated the array, what do you mean exactly? Commented Mar 6, 2011 at 19:14
  • I create the array int scores[] = {90, 95, 100} and then update the array scores[0]=80... Per the answer below I think I need to allocate new memory for it. Commented Mar 6, 2011 at 19:20

4 Answers 4

2

From what you describe:

You need to create a new object for each item in the array.

so first you create something to hold the first item (like a pointer to the first item). Then as you load each item, create new studentNode and then add it to the array.

Sign up to request clarification or add additional context in comments.

Comments

0

Make sure each studentNode isn't sharing the same programs, labs, etc arrays. In addition to creating the studentNode, you have to create the arrays within them.

Comments

0

I believe your question is referring to how to specify the size of the programs, labs, and exams arrays.

Instead of storing the array in your struc, keep a pointer to your array. Then, after you have built your program array outside of your structure (using malloc if necessary) reassign your node.program pointer to that array.

Comments

0

It sounds like you are using the same array for all students. You need to allocate a separate array for each student.

You can either statically allocate a fixed number of elements in a 2d array:

int progs[Num_students][x];

In this case you will need to point each studentNodes array to the correct array(for each student).

studentNode1.programs = progs[studentNumber];

Or allocate some memory on the heap for each student.

studentNode1.programs = malloc(sizeof(int)*x);

2 Comments

I believe the instructor wants us to allocate additional memory. I'll try your second suggestion.
don't cast the return value of malloc in C. it's unnecessary and it can silence compiler warnings which you might want to see. (the exception is in polyglot C/C++ code, where the cast is required.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.