Is this OK?



Good day,
Developers!

Since I'm new to C and GTK, and am lacking knowledge, can You look at
this, and tell me is it OK?

/*
 * H file
 */
typedef struct _KParent                 KParent;
typedef struct _KEducationProfile       KEducationProfile;
typedef struct _KStudent                KStudent;

struct _KParent {
        gchar *first_name;
        gchar *last_name;
        gchar *occupation;
        gchar *address;
};

struct _KeducationProfile {
        gchar *name;
        guint years;
        guint *subjects;
        GNode *subjects_container;
        GNode *grades_container;
};

struct _KStudent {
        KEducationProfile *profile;
        
        gchar *first_name;
        gchar *last_name;
        gchar *address;
        
        gchar *birth_state;
        gchar *birth_city;
        GDate *birth_date;
        
        KParent *father;
        KParent *mother;
        KParent *foster;
};

KStudent*       k_student_new                   ();

/*
 * C file
 */
#include "kstudent.h"

KStudent*
k_student_new ()
{
        KStudent *student = NULL;
        
        student = g_malloc (sizeof (KStudent));
        
        student->profile = NULL;
        student->profile = g_malloc (sizeof (KEducationPofile));
        student->profile->name = NULL;
        student->profile.years = 0;
        student->profile->subjects = NULL;
        student->profile->subjects_container = NULL;
        student->profile->grades_container = NULL;
        
        student->first_name = NULL;
        student->last_name = NULL;
        student->address = NULL;
        
        student->birth_state = NULL;
        student->birth_city = NULL;
        student->birth_date = NULL;
        
        student->father = NULL;
        student->father = g_malloc (sizeof (KParent));
        student->father->first_name = NULL;
        student->father->last_name = NULL;
        student->father->address = NULL;
        student->father->occupation = NULL;
        
        student->mother = NULL;
        student->mother = g_malloc (sizeof (KParent));
        student->mother->first_name = NULL;
        student->mother->last_name = NULL;
        student->mother->address = NULL;
        student->mother->occupation = NULL;
        
        student->foster = NULL;
        student->foster = g_malloc (sizeof (KParent));
        student->foster->first_name = NULL;
        student->foster->last_name = NULL;
        student->foster->address = NULL;
        student->foster->occupation = NULL;
        
        return student;
}

Is this the way to do it (create new KStudent)? Later, when I need to
free mem, I just check if these are __NOT__ NULL and free them. If not,
how should I create new KStudent? I need this for school app.

THANKS,
Vladimir Djokic.





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]