GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Oct 22, 2014 11:57 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Creating multiple new struct objects
PostPosted: Thu Dec 06, 2007 8:20 pm 
Offline

Joined: Sun Oct 21, 2007 3:20 pm
Posts: 3
I am writing a program that allows a user to select a student's name and shows the information of the student inside text entries. The information is in a file and I was thinking about using a struct to store information and using a hash table to get a student's info when the user selects a student.

I am having trouble implementing this because I am unsure how to create new student struct objects every time a new "student " is read from a file.
Can anyone post a suggestion on how I can get started? Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 06, 2007 9:39 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Maybe something along these lines. Note, I'm just typing this, it's note tested or anything:



Code:
typedef struct
{
  gchar *first_name;
  gchar *last_name;
  /* etc... */
} Student;

Student *
student_new (const gchar *first, const gchar *last)
{
  Student *s = g_slice_new (Student);
  s->first_name = g_strdup (first);
  s->last_name = g_strdup (last);

  /* 'first' and 'last' strings from calling function can be freed */

  return s;
}

void
student_free (Student *s)
{
  if (s->first_name) g_free (s->first_name);
  if (s->last_name) g_free (s->last_name);
  g_slice_free (Student, s);
}


_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group