Re: Array of arrays



On Sat, Feb 27, 2010 at 10:06:46PM -0500, Tristan Van Berkom wrote:
On Sat, Feb 27, 2010 at 9:50 PM, Brian Lavender <brian brie com> wrote:
I guess the list stripped the attachments. The code is included in this
message.


Hi,
    First of all it would be helpful if you told us what is the problem with
your code, off the bat I could tell you that the way you pass a pointer
to an array of pointers is foreign to me, I think I would have just used
"GArray ***arrays_p;" for that argument.

But on the other hand, you could just save yourself that headache and
use a GPtrArray of GArrays (you could even get carried away and whip
up an api that updates the values of ptrarray->pdata[i] = garray->data
and have a real indexable array in C...).

This would seems like the most robust solution, but before I go down
that path, let's see if I can figure this out. I have some other code
centered around it, and i would like to see if I can just push it
forward for now. 

Basically, my current problem is that I can't seem to create a
subroutine that will load the following arrays of arrays. How do I put
my load code in the subroutine?


#include <glib.h>

#define NUM_ARYS 5

void load_array( GArray *garrys[NUM_ARYS] )
{
  // put the load arrays stuff here.
}

int main() {
    GArray *garrays[NUM_ARYS];
  gint i,j, storevalue;

  // Change the following so that I call
  // load_arrays(garrays);
  // instead of the following

  // start

  for (j=0; j < NUM_ARYS; j++) {
    garrays[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 ( garrays[j], storevalue );
      g_print ("load idx %d value %d\n",
               i, storevalue );
    }
  }

  // end



  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 (garrays[j], gint, i));
  }

  for (j=0; j < NUM_ARYS; j++)
    g_array_free (garrays[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]