Array dimension error

Posted By: txesmi

Array dimension error - 01/19/12 16:53

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:
Code:
#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
Posted By: txesmi

Re: Array dimension error - 01/19/12 17:17

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

Code:
#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 );
}


Posted By: Uhrwerk

Re: Array dimension error - 01/19/12 19:14

Deleted by poster.
Posted By: JibbSmart

Re: Array dimension error - 01/20/12 01:23

Code:
char **CHR_Pointer;

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.
Posted By: txesmi

Re: Array dimension error - 01/20/12 20:13

Ok, thank you very much for the answers. I learned a bit more how arrays work.

I will need read more about how to access to the memory addresses pointed by the array...

have a nice day!
Posted By: txesmi

Re: Array dimension error - 01/21/12 09:02

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?

Code:
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
Posted By: JibbSmart

Re: Array dimension error - 01/21/12 20:12

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.
Posted By: vas

Re: Array dimension error - 01/04/17 20:16

Originally Posted By: JibbSmart
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
© 2024 lite-C Forums