g_slist_copy_deep的用法示例

google了半天,结果是没找到一个示例,后来自己瞎折腾,还真有结果了。下面贴代码,回头有时间了再解释下吧

#include <glib-2.0/glib.h>
#include <glib-2.0/glib/gprintf.h>
#include <stdio.h>
copyfunc(  )
{

}
int main(int argc, char * argv[])
{
  GSList * source_slist ;
  GSList * dest_slist = g_slist_alloc();
  GSList * dest1_slist = g_slist_alloc();
  gchar * temp = g_malloc ( 10 );
  g_stpcpy(temp, "第一個");
  source_slist = g_slist_append(source_slist, temp);
  g_stpcpy(temp, "第二個");
  source_slist = g_slist_append(source_slist, temp);
  temp = g_strdup("第三個"  );
  source_slist = g_slist_append(source_slist, temp);
  dest1_slist = g_slist_copy_deep(source_slist, ( GCopyFunc )g_strdup,NULL );
  g_printf( "the first string is %s\n", (g_slist_nth(source_slist, 0))->data );
  g_printf( "the second string is %s\n", g_slist_nth_data(source_slist, 1) );
  g_printf( "the third string is %s\n", g_slist_nth_data(source_slist, 2) );
  dest_slist = g_slist_copy(source_slist);
   g_stpcpy(temp, "tmp");
  g_printf( "the first string is %s\n", (g_slist_nth(dest_slist, 0))->data );
  g_printf( "the second string is %s\n", g_slist_nth_data(dest_slist, 1) );
  g_printf( "the third string is %s\n", g_slist_nth_data(dest_slist, 2) );

  g_printf( "the first string is %s\n", (g_slist_nth(dest1_slist, 0))->data );
  g_printf( "the second string is %s\n", g_slist_nth_data(dest1_slist, 1) );
  g_printf( "the third string is %s\n", g_slist_nth_data(dest1_slist, 2) );
  g_free( temp );
  g_slist_free(source_slist);
  g_slist_free(dest_slist);
  g_slist_free(dest1_slist);
}

添加新评论