Guest User

Untitled

a guest
Dec 15th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #include <glib.h>
  2. #include <stdio.h>
  3.  
  4. int main( int argc, char *argv[] )
  5. {
  6. GString *s;
  7. GString gs;
  8. GArray *a;
  9. int i;
  10.  
  11. a = g_array_sized_new( FALSE, FALSE, sizeof(GString *), 100 );
  12. for( i=0; i<100; i++ )
  13. {
  14. s = g_string_new( "element" );
  15. g_string_append_printf( s, ",%d", i );
  16. g_array_append_val( a, s );
  17. printf( "inserting %s\n", s->str );
  18. }
  19. s = g_array_index( a, GString *, 40 );
  20. printf( "retrieving %s\n", s->str );
  21. }
  22.  
  23. /* an alternative way is */
  24.  
  25. int main( int argc, char *argv[] )
  26. {
  27. GString *s;
  28. GString gs;
  29. GArray *a;
  30. int i;
  31.  
  32. a = g_array_sized_new( FALSE, FALSE, sizeof(GString), 100 );
  33. for( i=0; i<100; i++ )
  34. {
  35. s = g_string_new( "element" );
  36. g_string_append_printf( s, ",%d", i );
  37. g_array_append_val( a, *s );
  38. printf( "inserting %s\n", s->str );
  39. }
  40. gs = g_array_index( a, GString, 40 );
  41. printf( "retrieving %s\n", gs.str );
  42. }
  43.  
  44. /* This second way is worse. There's more mental pointer math happening. */
Add Comment
Please, Sign In to add comment