2d array problem

Discuss PocketC for Palm OS (including Desktop Edition)

Postby gmyx on Sun Nov 05, 2006 1:05 pm

I'm trying to get dynamic 2d arrays to work, but all I get is the lst index of array 1 repeted at all indexes (see results below)

First the code: (using Array_2d from UtilCode folder of PocketC Distribution)

*** BEGIN CODE ***
//test 2d array

include "array2d"

pointer giArray;

main()
{
int x, y, i;

clear();
*giArray=array2d(2, 2, "i");
puts(*giArray+"\n");
for (x=0;x!=2;x++)
{
for (y=0;y!=2;y++)
{
giArray[x][y]=i;
puts(x +", " + y +", " + i + ", " +giArray[x][y]+ ", " + "\n" );
i++;
}
}

for (x=0;x!=2;x++)
{
for (y=0;y!=2;y++)
{
puts(x +", " + y +", " + giArray[x][y] + ", " + "\n" );
i++;
}
}
}
*** END CODE ***

And the Results:
*** BEGIN RESULTS ***
0, 0, 0, 0
0, 1, 1, 1
1, 0, 2, 2
1, 1, 3, 3
0, 0, 2
0, 1, 3
1, 0, 2
1, 1, 3
*** END RESULTS ***

What is groing wrong here? Any help is apreciated.

In case you don't have array2d (trimmed exess comments), here is it's code
*** BEGIN CODE ***
/$ array2d
array2d(int dim1, int dim2, char atype) {
pointer base_array, index_array;
int i;

base_array = malloct(dim1 * dim2, atype);
if (!base_array)
return 0;
index_array = malloct(dim1, "p");
if (!index_array) {
free(base_array);
return 0;
}
for (;i<dim1;i++)
index_array[i] = &base_array[dim2 * i];
return index_array;
}
*** END CODE ***
gmyx
 
Posts: 8
Joined: Thu Nov 02, 2006 4:39 pm

Postby gmyx on Tue Nov 07, 2006 10:53 pm

I`ve fixed this one my self. I changed the data type of my array from pointer to int and it works no problem.
gmyx
 
Posts: 8
Joined: Thu Nov 02, 2006 4:39 pm


Return to PocketC for Palm OS

Who is online

Users browsing this forum: No registered users and 1 guest

cron