Re: Array of arrays



Turns out that my simplearray2.c did work! But when I printed out the
arrays, I forget to make the subscript so that it referenced the different
arrays, but instead just one!

See correction below

Now that recall you can pass an array directly, I won't be using this
function prototype to pass arguments!

void load_array( GArray *(*garray)[NUM_ARYS] );

I will check out the g_ptr_array!

brian

On Sat, Feb 27, 2010 at 06:50:34PM -0800, Brian Lavender wrote:

=== 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));
                i, g_array_index (garray[j], gint, i));

  }

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

}

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

"There are two ways of constructing a software design. One way is to
make it so simple that there are obviously no deficiencies. And the other
way is to make it so complicated that there are no obvious deficiencies."

Professor C. A. R. Hoare
The 1980 Turing award lecture



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