Gamestudio Links
Zorro Links
Newest Posts
MT5 bridge not working on MT5 v. 5 build 4160
by EternallyCurious. 04/25/24 20:49
Data from CSV not parsed correctly
by EternallyCurious. 04/25/24 10:20
Trading Journey
by howardR. 04/24/24 20:04
M1 Oversampling
by Petra. 04/24/24 10:34
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (EternallyCurious, AndrewAMD, TipmyPip, Quad), 889 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
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 Offline OP
Serious User
txesmi  Offline 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:
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

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 Offline OP
Serious User
txesmi  Offline 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...

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 );
}



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 Offline
Expert
Uhrwerk  Offline
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
J
JibbSmart Offline
Expert
JibbSmart  Offline
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
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.


Formerly known as JulzMighty.
I made KarBOOM!
Re: Array dimension error [Re: JibbSmart] #392263
01/20/12 20:13
01/20/12 20:13
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline OP
Serious User
txesmi  Offline OP
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
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!

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 Offline OP
Serious User
txesmi  Offline 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?

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

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
J
JibbSmart Offline
Expert
JibbSmart  Offline
Expert
J

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
V
vas Offline
Guest
vas  Offline
Guest
V

Joined: Jan 2017
Posts: 2
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


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1