Search code examples
cdata-structuresinitializationdoubly-linked-list

How to properly initialize a big structure with doubly linked list?


I am practicing linked lists by attempting to make a simple school structure, I keep error checking as I go and noticed this:

$ gcc list.c -Wall
list.c: In function ‘newStudent’:
list.c:52:15: warning: variable ‘newNode’ set but not used [-Wunused-but-set-variable]
  studentList *newNode;
               ^~~~~~~
list.c: In function ‘initLists’:
list.c:36:14: warning: ‘auxSt’ is used uninitialized in this function [-Wuninitialized]
  auxSt->name = NULL;
              ^
list.c:41:14: warning: ‘auxR’ is used uninitialized in this function [-Wuninitialized]
  auxR->names = NULL;
              ^
list.c:46:16: warning: ‘school’ is used uninitialized in this function [-Wuninitialized]
  school->rooms = NULL;
                ^


This is my code so far...

typedef struct roomList roomList;
typedef struct school school;
typedef struct studentList studentList;

struct studentList
{

    char *name;
    int  grade;
    studentList *next;
    studentList *prev;
};

struct roomList
{   
    int class;
    int nrOfStudents;
    studentList *names;
    roomList *next;
    roomList *prev; 
};

struct school
{
    int totalStudents;
    roomList *rooms;
};

void initLists()
{
    studentList *auxSt;
    auxSt->name = NULL;
    auxSt->next = auxSt->prev = NULL;
    auxSt = NULL;

    roomList *auxR;
    auxR->names = NULL;
    auxR->next = auxR->prev = NULL;
    auxR = NULL;

    school *school;
    school->rooms = NULL;
    school = NULL;
}


int main()
{
    initLists();

     return 0;
}

But it appears that after I initialize my aux variables first with NULL before going to the members this warning disappears. But I have concerns over initializing the aux variables with NULL before its own members. Am I on the right path for this?

What is the advantage of using an init()function over initializing as you're inserting? Haven't seen almost a single example online with this function.


Solution

  • Let's have a look at the first function.

    studentList *newStudent(int class, char *name)
    {
        studentList *newNode;
        newNode = (studentList*)malloc(sizeof(studentList));
    
        return NULL;     
    }
    

    You are not returning anything. Well, you return NULL pointer. The whole function is equivalent to

    studentList *newStudent(int class, char *name)
    {
        return NULL;     
    }
    

    with the only exception that your code causes an extra memory leak. You need to replace return NULL with return newNode. Also, never cast malloc and use the var instead of type as arg for sizeof. Just use newNode = malloc(sizeof(*newNode))

    The function initLists has similar problems. You create a couple of variables, but you don't use them in a way that matters after the function is done. You also try to dereference unallocated pointers.

    Am I on the right path for this?

    Not really. I would suggest studying pointers and memory allocation a bit more, and search for example code for doubly linked lists.