|
Array dimension error
#392136
01/19/12 16:53
01/19/12 16:53
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
OP
Serious User
|
OP
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
Hi friends, I have been trying to use a pointer to a multidimensional array but the compiler returns an array dimension error when I try to use the array dimensions with the pointer. I focused this code to the problem:
#include <acknex.h>
int INT_CalculeIndex ( int IndexX, int IndexY )
{
return ( (IndexX*8)+IndexY );
}
function main ()
{
wait(3);
STRING *stemp = str_create ( "" );
int IndexY, IndexX, CalculatedIndex;
char CHR_Array[8][8];
char *CHR_Pointer;
CHR_Pointer = CHR_Array;
for ( IndexX=0; IndexX<8; IndexX++ )
{
for ( IndexY=0; IndexY<8; IndexY++ )
{
CalculatedIndex = INT_CalculeIndex ( IndexX, IndexY );
// I can assign values to the array with its dimensions
CHR_Array[IndexX][IndexY] = CalculatedIndex;
// also calculating a unique index
CHR_Array[CalculatedIndex] = CalculatedIndex;
// I can assign values to the array using the pointer and calculating a unique index
CHR_Pointer[CalculatedIndex] = CalculatedIndex;
// but not using the array dimensions, compiler returns a dimension of array error
//CHR_Pointer[IndexX][IndexY] = CalculatedIndex;
}
}
while ( !key_esc )
{
for ( IndexX=0; IndexX<8; IndexX++ )
{
for ( IndexY=0; IndexY<8; IndexY++ )
{
CalculatedIndex = INT_CalculeIndex ( IndexX, IndexY );
draw_text ( str_for_int(NULL,CHR_Array[CalculatedIndex]), IndexX*30, IndexY*30, COLOR_WHITE );
draw_text ( str_for_int(NULL,CHR_Pointer[CalculatedIndex]), 300+(IndexX*30), IndexY*30, COLOR_WHITE );
draw_text ( str_for_int(NULL,CHR_Array[IndexX][IndexY]), IndexX*30, 300+(IndexY*30), COLOR_WHITE );
// same compiler dimension of array error
//draw_text ( str_for_int(NULL,CHR_Array[IndexX][IndexY]), 300+(IndexX*30), 300+(IndexY*30), COLOR_WHITE );
}
}
wait(1);
}
sys_exit ( NULL );
}
I think it should work but i does not. Could someone throw a bit of light to my mixed knowledge? Why does not work? thanks in advance, txes
|
|
|
Re: Array dimension error
[Re: txesmi]
#392137
01/19/12 17:17
01/19/12 17:17
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
OP
Serious User
|
OP
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
ok, i found that i can copy the content of the array to a new declared multidimensinal array to use its dimensions, but i still think that the topic code exmaple should work...
#include <acknex.h>
int INT_CalculeIndex ( int IndexX, int IndexY )
{
return ( (IndexX*8)+IndexY );
}
function main ()
{
wait(3);
STRING *stemp = str_create ( "" );
int IndexY, IndexX, CalculatedIndex;
char CHR_Array[8][8];
for ( IndexX=0; IndexX<8; IndexX++ )
{
for ( IndexY=0; IndexY<8; IndexY++ )
{
CalculatedIndex = INT_CalculeIndex ( IndexX, IndexY );
// I can assign values to the array with its dimensions
//CHR_Array[IndexX][IndexY] = CalculatedIndex;
// also calculating a unique index
CHR_Array[CalculatedIndex] = CalculatedIndex;
}
}
char *CHR_Pointer;
//CHR_Pointer2 = sys_malloc ( sizeof(char) * 64 );
CHR_Pointer = CHR_Array;
char CHR_Array2[8][8];
memcpy ( CHR_Array2, CHR_Pointer, sizeof(char)*64 );
while ( !key_esc )
{
for ( IndexX=0; IndexX<8; IndexX++ )
{
for ( IndexY=0; IndexY<8; IndexY++ )
{
CalculatedIndex = INT_CalculeIndex ( IndexX, IndexY );
draw_text ( str_for_int(NULL,CHR_Array[CalculatedIndex]), IndexX*30, IndexY*30, COLOR_WHITE );
draw_text ( str_for_int(NULL,CHR_Array[IndexX][IndexY]), 300+(IndexX*30), IndexY*30, COLOR_WHITE );
draw_text ( str_for_int(NULL,CHR_Array2[CalculatedIndex]), IndexX*30, 300+(IndexY*30), COLOR_WHITE );
draw_text ( str_for_int(NULL,CHR_Array2[IndexX][IndexY]), 300+(IndexX*30), 300+(IndexY*30), COLOR_WHITE );
}
}
wait(1);
}
sys_exit ( NULL );
}
|
|
|
Re: Array dimension error
[Re: txesmi]
#392146
01/19/12 19:14
01/19/12 19:14
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
Deleted by poster.
Last edited by Uhrwerk; 01/20/12 23:37. Reason: Information given was wrong
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Array dimension error
[Re: Uhrwerk]
#392201
01/20/12 01:23
01/20/12 01:23
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
Two dimensions, two asterisks. Remember, an array is a pointer, and a pointer is an array (as you indicated already). A 2D array is an array of arrays, or an array of pointers, or a pointer to a pointer. INT_CalculateIndex is a dangerous function, because that's not how multidimensional arrays work. When you do Array[x], the compiler converts it to *(Array + x). When you do Array[x][y], the compiler converts it to *(*(Array + x) + y). You cannot reach your target with only one index.
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: Array dimension error
[Re: txesmi]
#392290
01/21/12 09:02
01/21/12 09:02
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
OP
Serious User
|
OP
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
Hi, another question to ensure that I understood fine... If I store the array in a pointer to a pointer and increment it in one, should it store the address of the next pointers array?
char Array[2][2];
char **PointerToPointer;
char *Pointer;
PointerToPointer = &Array + 1;
Pointer = &Array + 1;
// expected in my headbreaks
*PointerToPointer == &Array[1][0] // address of next pointer array
*Pointer == Array[0][1] // next address into the array
// but it allways is
*PointerToPointer == Array[0][1] // next address into the array
*Pointer == Array[0][1] // next address into the array
What I did wrong? thanks in advance, txes
|
|
|
Re: Array dimension error
[Re: txesmi]
#392344
01/21/12 20:12
01/21/12 20:12
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
Okay, well... apparently I'm wrong. I mean, apparently C treats multi-dimensional arrays and pointers to pointers differently, even though they both use "[][]", and Lite-C treats them a little differently again.
The compiler uses the Array[x][y] -> *(*(Array + x) + y) conversion for pointers, and so it's not uncommon to allocate dynamic arrays in that way so the same syntax can be used. But when you declare an array (rather than create one manually), it works more like Uhrwerk first said.
Sorry about that.
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: Array dimension error
[Re: JibbSmart]
#463900
01/04/17 20:16
01/04/17 20:16
|
Joined: Jan 2017
Posts: 2
vas
Guest
|
Guest
Joined: Jan 2017
Posts: 2
|
Okay, well... apparently I'm wrong. I mean, apparently C treats multi-dimensional arrays and pointers to pointers differently, even though they both use "[][]", and Lite-C treats them a little differently again.
Where I can find about how exactly lite-C treats multi-dimensional arrays? Thanks
|
|
|
|