Thread: How would I qsort in this case

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    How would I qsort in this case

    I'm sorting a utmp file by ut_time. Right now, I have something like the following:

    Code:
    #define MAX_USERS 10
    
    struct utmp info[MAX_USERS];
    
    int 
    compare (const void *a, const void *b)
    {
      const struct utmp *ma = a;
      const struct utmp *mb = b;
      int32_t time1, time2;
      time1 = ma->ut_time; 
      time2 = mb->ut_time;
     
      if(time1 < time2)
          return -1;
      else if(time1 > time2)
          return 1;
      else
          return 0;
    }
    
    void 
    print_session(void)
    {
     int i;
     for(i=0; i < counter; i++)
          printf("name: %s, tty: %s\n", info[i].ut_name, info[i].ut_line);
    }
    
    
    void 
    find_sessions_to_kill(void){
      qsort(info,
      counter,
      sizeof(struct utmp),
      compare
       );
      print_session();
      kill_sessions();
    }
    Now, how I qsort ut_time in utmp if I had a structure like the following:
    Code:
    typedef struct {
      uid_t uid; /*From the uid I can get the pid*/
      dev_t dev;
      struct utmp in_users;
    }uinfo_t;
    
    uinfo_t uinfo[MAX_USERS];
    I would assume ut_time would be found by
    Code:
    uinfo.in_users.ut_time

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > uinfo.in_users.ut_time
    Try
    uinfo->in_users.ut_time
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault!?!?!?
    By Viper187 in forum Windows Programming
    Replies: 57
    Last Post: 10-02-2008, 09:40 PM
  2. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. Creating pop up menus
    By Ti22 in forum C++ Programming
    Replies: 22
    Last Post: 01-18-2005, 09:27 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM