I guess the list stripped the attachments. The code is included in this
message.

brian

On Sat, Feb 27, 2010 at 06:47:30PM -0800, Brian Lavender wrote:
> I was experimenting with creating an array of arrays. Maybe I shouldn't
> be using GArray but something different such as pointer arrays and
> allocating memory for each element? 
> 
> I wrote two sample programs. The first one, just loads one array. The
> second, loads an array of arrays. I don't know if I have a problem with
> operator precedence or if the GArrays point to just one array. 
> 
> The two programs are the following.
> 
> simplearray.c - loads just one array
> simplearray2.c - loads an array of arrays
> 
> Any input is appreciated.

=== simplearray.c ===

#include <glib.h>

#define NUM_ARYS 5

void load_array( GArray **garray)
{
  gint i, storevalue;
  *garray = g_array_new (FALSE, FALSE, sizeof (gint));
  for (i = 0; i < 10; i++) {
    storevalue = (i + 103) % 45;
    g_array_append_val (*garray, storevalue);
  }
}

int main() {
  GArray *garray[NUM_ARYS];
  gint i, storevalue;
  /* We create a new array to store gint values.
     We don't want it zero-terminated or cleared to 0's. */
  load_array(&garray[0]);

  for (i = 0; i < 10; i++)
      g_print ("index %d value %d\n",
               i, g_array_index (garray[0], gint, i));
  g_array_free (garray[0], TRUE);
}

=== simplearray2.c ===


#include <glib.h>

#define NUM_ARYS 5

void load_array( GArray *(*garray)[NUM_ARYS] )
{
  gint i,j, storevalue;
  for (j=0; j < NUM_ARYS; j++) {
    (*garray)[j] = g_array_new (FALSE, FALSE, sizeof (gint));
    g_printf("Load Array %d\n", j);
    for (i = 0; i < 10; i++) {
      storevalue = (i + 103) % ( (j +1) * 2 );
      g_array_append_val ( (*garray)[j], storevalue );
      g_print ("load idx %d value %d\n",
               i, storevalue );
    }
  }
}

int main() {
  GArray *garray[NUM_ARYS];
  gint i,j, storevalue;
  /* We create a new array to store gint values.
     We don't want it zero-terminated or cleared to 0's. */
  load_array(&garray);

  for (j=0; j < NUM_ARYS; j++) {
    g_printf("Array %d\n", j);
    for (i = 0; i < 10; i++)
      g_print ("index %d value %d\n",
               i, g_array_index (garray[1], gint, i));
  }

  for (j=0; j < NUM_ARYS; j++)
    g_array_free (garray[j], TRUE);

}

-- 
Brian Lavender
http://www.brie.com/brian/

"About 3 million computers get sold every year in China, but people don't
pay for the software. Someday they will, though. As long as they are going
to steal it, we want them to steal ours. They'll get sort of addicted, and
then we'll somehow figure out how to collect sometime in the next decade."

-- Bill Gates (Microsoft) 1998
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Reply via email to