String equivalence in glib functions (such as g_slist_remove)



Hi all.

Newbie question here: the following program doesn't do what I want it
to do (which is to remove the "second" list item):



#include <stdio.h>
#include <glib.h>
#include <string.h>

int main(int argc, char** argv) {

  GSList *list = NULL, *iterator = NULL;
  list = g_slist_append (list, g_strdup("first"));
  list = g_slist_append (list, g_strdup("second"));
  list = g_slist_append (list, g_strdup("third"));
  list = g_slist_append (list, g_strdup("fourth"));

  printf("Before...\n");
  for (iterator = list; iterator; iterator = iterator->next) {
    printf("Current item is '%s'\n", iterator->data);
  }

  list = g_slist_remove (list, g_strdup("second"));

  printf("After...\n");
  for (iterator = list; iterator; iterator = iterator->next) {
    printf("Current item is '%s'\n", iterator->data);
  }

  return 0;
}



Its output is:

Before...
Current item is 'first'
Current item is 'second'
Current item is 'third'
Current item is 'fourth'
After...
Current item is 'first'
Current item is 'second'
Current item is 'third'
Current item is 'fourth'

I assume that this is because the dynamically allocated strings aren't
"the same" (i.e. they're at different places in memory). What do
people generally do when their idea of equivalence isn't the same as
"=="? Is there some way of getting glib's functions to use an
"equivalence function" rather than the "hardcoded" "=="?

Hope this makes sense, Jaime :-)



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