Hi Brian,<br><br>Wishing you all the best on your path! :)<br><br>simplearray2.c::load_arrays() seems to expect a GArray ** (pointer to a pointer; or, pointer to an array of pointers).<br><br>simplearray4.c::load_arrays() seems to expect a GArray *** (pointer to a
pointer to a pointer; or, pointer to a pointer to an array of pointers).<br>
<br>The latter case is handy if you needed to alloc memory for the GArray array of pointers and optionally for each pointer within the array.<br><br>The former case is handy if you only need to alloc memory for each pointer within the array. I.e., the array is already alloc'd.<br>
<br>Not sure if you are requesting clarification, but here goes:<br><br>simplearray2.c::main():<br><br><div style="margin-left: 40px;">garrays_ptr -> (beginning of unallocated array of pointers)<br><br>...<br><br>call to load_arrays()<br>
<br><div style="margin-left: 40px;">copy_of_garrays_ptr -> (beginning of unallocated array of pointers)<br><br>...<br></div><br>return from load_arrays() -- copy_of_garrays_ptr destroyed<br><br>g_arrays_ptr -> (beginning of allocated array of pointers)<br>
<br></div><br>simplearray4.c::main():<br><br>garrays_ptr_ptr -> garrays_ptr -> (beginning of unallocated array of pointers)<br>
<br>
...<br>
<br>
call to load_arrays()<br>
<br>
<div style="margin-left: 40px;">copy_of_garrays_ptr_ptr -> garrays_ptr -> (beginning of
unallocated array of pointers)<br>
<br>
...<br>
</div>
<br>
return from load_arrays() -- copy_of_garrays_ptr_ptr destroyed<br>
<br>
garrays_ptr_ptr -> garrays_ptr -> (beginning of unallocated array
of pointers)<br><br><br>Not sure this helps or makes it more obscure. :X<br><br><br><div class="gmail_quote">On Sun, Feb 28, 2010 at 9:00 PM, Brian Lavender <span dir="ltr"><<a href="mailto:brian@brie.com">brian@brie.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">I am ranting because of strange experiences like the following two<br>
programs, simplearray2.c and simplearray4.c. They both do the same<br>
thing! But look at the argument to load_arrays in version 2 vs 4.<br>
<br>
You can compile them with the following (you have to have libglib-2.0<br>
installed for Ubuntu at least).<br>
<br>
gcc `pkg-config --libs --cflags glib-2.0` simplearray2.c -o simplearray2<br>
<br>
Yes, you are correct, I am on a pilgrimage. Right now, it's for my<br>
Master's Project!<br>
<br>
brian<br>
<br>
<br>
=== simplearray2.c ===<br>
<br>
#include <glib.h><br>
<br>
#define NUM_ARYS 5<br>
<br>
void load_arrays( GArray *garrays[NUM_ARYS] )<br>
{<br>
gint i,j, storevalue;<br>
// put the load arrays stuff here.<br>
for (j=0; j < NUM_ARYS; j++) {<br>
garrays[j] = g_array_new (FALSE, FALSE, sizeof (gint));<br>
g_printf("Load Array %d\n", j);<br>
for (i = 0; i < 10; i++) {<br>
storevalue = (i + 103) % ( (j +1) * 2 );<br>
g_array_append_val ( garrays[j], storevalue );<br>
g_print ("load idx %d value %d\n",<br>
i, storevalue );<br>
}<br>
}<br>
<br>
}<br>
<br>
int main() {<br>
GArray *garrays[NUM_ARYS];<br>
gint i,j, storevalue;<br>
<br>
// Change the following so that I call<br>
// load_arrays(garrays);<br>
// instead of the following<br>
<br>
// start<br>
load_arrays(garrays);<br>
<br>
// end<br>
<br>
for (j=0; j < NUM_ARYS; j++) {<br>
g_printf("Array %d\n", j);<br>
for (i = 0; i < 10; i++)<br>
g_print ("index %d value %d\n",<br>
i, g_array_index (garrays[j], gint, i));<br>
}<br>
<br>
for (j=0; j < NUM_ARYS; j++)<br>
g_array_free (garrays[j], TRUE);<br>
<br>
}<br>
<br>
=== simplearray4.c ===<br>
<br>
<br>
#include <glib.h><br>
<br>
#define NUM_ARYS 5<br>
<br>
void load_array( GArray *(*garray)[NUM_ARYS] )<br>
{<br>
gint i,j, storevalue;<br>
for (j=0; j < NUM_ARYS; j++) {<br>
(*garray)[j] = g_array_new (FALSE, FALSE, sizeof (gint));<br>
g_printf("Load Array %d\n", j);<br>
for (i = 0; i < 10; i++) {<br>
storevalue = (i + 103) % ( (j +1) * 2 );<br>
g_array_append_val ( (*garray)[j], storevalue );<br>
g_print ("load idx %d value %d\n",<br>
i, storevalue );<br>
}<br>
}<br>
}<br>
<br>
int main() {<br>
GArray *garray[NUM_ARYS];<br>
gint i,j, storevalue;<br>
/* We create a new array to store gint values.<br>
We don't want it zero-terminated or cleared to 0's. */<br>
load_array(&garray);<br>
<br>
for (j=0; j < NUM_ARYS; j++) {<br>
g_printf("Array %d\n", j);<br>
for (i = 0; i < 10; i++)<br>
g_print ("index %d value %d\n",<br>
i, g_array_index (garray[j], gint, i));<br>
}<br>
<br>
for (j=0; j < NUM_ARYS; j++)<br>
g_array_free (garray[j], TRUE);<br>
<div class="im"><br>
}<br>
<br>
On Sun, Feb 28, 2010 at 08:03:14PM -0800, Kevin Schultz wrote:<br>
> Hi Brian,<br>
> Sounds like you are the path to a true religious experience! ;D<br>
> mod_array() also receives a pass-by-value argument. ;) C copies the<br>
> pointer. Variable int b[CAP] is really syntactic sugar for int * b. And<br>
> so is mod_array()'s formal parameter int a[], which is the same as int<br>
> * a. So the actual and formal parameters match up. And since you are<br>
> changing what the pointer points to, all is well when the function<br>
> returns.<br>
> However, if you pass an array (say, int a[]) into a function, and then<br>
> attempt to change what variable a points to, it will last only as long<br>
> as you are in the function, because a is a copy of the original<br>
> pointer. For example, a = calloc(4, sizeof(char));. When the function<br>
> ends, variable a is deleted, control returns to main(), and pointer b<br>
> still points to its original contents. To get around this, you need to<br>
> pass a pointer to a pointer into the function (int **b). :P<br>
> I am likely proving your point about C being a detriment to the field!<br>
> ;D Let us know if/when you have a true Lisp epiphany. :) I hear it is<br>
> quite a revolution in thinking!<br>
><br>
</div>> On Sun, Feb 28, 2010 at 6:35 PM, Brian Lavender <[1]<a href="mailto:brian@brie.com">brian@brie.com</a>><br>
<div><div></div><div class="h5">> wrote:<br>
><br>
> I think if anything, C has been a certain detriment to the field of<br>
> computer science!<br>
> One calls a function and the arguments are passed by value. Call a<br>
> function with an array as an argument, and feel free to modify its<br>
> contents!<br>
> Certainly, C++ added the idea of reference, but I think Pascal<br>
> simplifies these concepts much better. Yet, Pascal seems to be<br>
> relegated<br>
> to the status as a legacy language!<br>
> brian<br>
> #include <stdio.h><br>
> #define CAP 10<br>
> void mod_array(int a[])<br>
> {<br>
> a[2] = 5;<br>
> }<br>
> void trychange(int a)<br>
> {<br>
> a = 2;<br>
> }<br>
> void reallychange(int *a)<br>
> {<br>
> *a = 2;<br>
> }<br>
> int main() {<br>
> int b[CAP];<br>
> int c;<br>
> int i;<br>
> printf("Load array and change a value\n");<br>
> for (i=0; i < CAP; i++)<br>
> b[i] = i + 20;<br>
> mod_array(b);<br>
> for (i=0; i < CAP; i++)<br>
> printf("b[%d] has value of %d\n",i,b[i]);<br>
> c = 10;<br>
> printf("c has a value of %d\n",c);<br>
> trychange(c);<br>
> printf("c has a value of %d after trychange(c)\n",c);<br>
> reallychange(&c);<br>
> printf("c has a value of %d after reallychange(&c)\n",c);<br>
> }<br>
> --<br>
> Brian Lavender<br>
</div></div>> [2]<a href="http://www.brie.com/brian/" target="_blank">http://www.brie.com/brian/</a><br>
<div class="im">> "There are two ways of constructing a software design. One way is to<br>
> make it so simple that there are obviously no deficiencies. And the<br>
> other<br>
> way is to make it so complicated that there are no obvious<br>
> deficiencies."<br>
> Professor C. A. R. Hoare<br>
> The 1980 Turing award lecture<br>
> _______________________________________________<br>
> vox mailing list<br>
</div>> [3]<a href="mailto:vox@lists.lugod.org">vox@lists.lugod.org</a><br>
> [4]<a href="http://lists.lugod.org/mailman/listinfo/vox" target="_blank">http://lists.lugod.org/mailman/listinfo/vox</a><br>
><br>
> References<br>
><br>
> 1. mailto:<a href="mailto:brian@brie.com">brian@brie.com</a><br>
> 2. <a href="http://www.brie.com/brian/" target="_blank">http://www.brie.com/brian/</a><br>
> 3. mailto:<a href="mailto:vox@lists.lugod.org">vox@lists.lugod.org</a><br>
> 4. <a href="http://lists.lugod.org/mailman/listinfo/vox" target="_blank">http://lists.lugod.org/mailman/listinfo/vox</a><br>
<div class="im"><br>
> _______________________________________________<br>
> vox mailing list<br>
> <a href="mailto:vox@lists.lugod.org">vox@lists.lugod.org</a><br>
> <a href="http://lists.lugod.org/mailman/listinfo/vox" target="_blank">http://lists.lugod.org/mailman/listinfo/vox</a><br>
<br>
<br>
</div>--<br>
<div class="im">Brian Lavender<br>
<a href="http://www.brie.com/brian/" target="_blank">http://www.brie.com/brian/</a><br>
<br>
</div>"About 3 million computers get sold every year in China, but people don't<br>
pay for the software. Someday they will, though. As long as they are going<br>
to steal it, we want them to steal ours. They'll get sort of addicted, and<br>
then we'll somehow figure out how to collect sometime in the next decade."<br>
<br>
-- Bill Gates (Microsoft) 1998<br>
<div><div></div><div class="h5">_______________________________________________<br>
vox mailing list<br>
<a href="mailto:vox@lists.lugod.org">vox@lists.lugod.org</a><br>
<a href="http://lists.lugod.org/mailman/listinfo/vox" target="_blank">http://lists.lugod.org/mailman/listinfo/vox</a><br>
</div></div></blockquote></div><br>