Re: Array of arrays



Sorry, the second set of code is what I want to do, but if you run it,
the arrays have all the same values, instead of the values actually
inserted, not quite what I had wanted.

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...).

Cheers,
          -Tristan

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


-- 
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



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