Gamestudio Links
Zorro Links
Newest Posts
Executing Trades on Next Bar Open
by vicknick. 06/13/24 08:51
Zorro Beta 2.61: PyTorch
by jcl. 06/10/24 14:42
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (AndrewAMD, mciwjd545, 1 invisible), 787 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19058 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 3 1 2 3
Multidimensional Array of STRINGS #386344
11/01/11 22:06
11/01/11 22:06
Joined: Dec 2010
Posts: 66
München (Germany)
dice Offline OP
Junior Member
dice  Offline OP
Junior Member

Joined: Dec 2010
Posts: 66
München (Germany)
Hi everyone,

I already know how to create an one-dimensional Array of Strings:
Code:
myString = malloc((int) sizeof(STRING*) * 5);
for(int i = 0; i < 5; i++)
    myString[i] = str_create("");



But how to create a multidimensional Array with this method? I tried sth like this:
Code:
myString = malloc((int) sizeof(STRING*) * 5);
for(int i = 0; i < 5; i++)
    myString[i] = str_create("");
	
for(int i = 0; i < 5; i++) {
    myString[i] = malloc((int) sizeof(STRING*) * 5);
    for(int k = 0; k < 5; k++)
        myString[i][k] = str_create("");
}


But it wont work...

Help me please ;D

GreetZ
dice!

Last edited by dice; 11/01/11 22:06.
Re: Multidimensional Array of STRINGS [Re: dice] #386373
11/02/11 06:33
11/02/11 06:33
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Code:
STRING** mystring;   //note the DOUBLE-'*'

myString = malloc(sizeof(STRING**) * 5);
for(int y = 0; y < 5; y++)
{   mystring[y] = malloc(sizeof(STRING*) * 5);
    for(int x = 0; x < 5; x++)
    {   (mystring[x])[y] = str_create("");   }
}

// and you need to use the "(mystring[x])[y]" format to access them

// because the format "mystring[x][y]" only works with pre-defined global arrays




"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Multidimensional Array of STRINGS [Re: EvilSOB] #396999
03/13/12 09:15
03/13/12 09:15
Joined: Mar 2011
Posts: 3,150
Budapest
sivan Offline
Expert
sivan  Offline
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
it was a great help for me to make dynamic 2D arrays, thanks! I don't know why such things are missing from the manual... (I tried it earlier too, but without success, because those extra brackets were missing, since they are not required in C/C++)


Free world editor for 3D Gamestudio: MapBuilder Editor
Re: Multidimensional Array of STRINGS [Re: sivan] #397000
03/13/12 09:30
03/13/12 09:30
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
If your version of 3DGS supports it, you are better of using
SYS_malloc() and SYS_free() instead of malloc() and free().

They have EXACTLY the same syntax and usage, but are somewhat 'managed'
by the engine.

I cant remember exactly, but I think they do more error-checking,
and they get automatically 'cleaned-out' by the engine on exit.
... I think ... Its been a while...

FYI: By following the same pattern, you can do 3, 4, or more dimensional arrays too.
eg: STRING**** data; //referenced by (((data[w])[x])[y])[z]=0;




"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Multidimensional Array of STRINGS [Re: EvilSOB] #397002
03/13/12 09:47
03/13/12 09:47
Joined: Mar 2011
Posts: 3,150
Budapest
sivan Offline
Expert
sivan  Offline
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
thanks, I use it not for strings, but works.
I know and always use sys_malloc because it's really safe, if no realloc is needed afterwards...
and maybe I'm now able to simulate an n-dimesnional space travel laugh laugh (I completely forgot what I learnt about n-dimensional space mathematics at university...)
I just put a post at blame the manual to add an example of this situation, because I think it is really useful!


Free world editor for 3D Gamestudio: MapBuilder Editor
Re: Multidimensional Array of STRINGS [Re: sivan] #397005
03/13/12 09:59
03/13/12 09:59
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
BTW, I guess you are already aware, it works on basically any data type.

I mostly use it for entities, longs, and custom structs.





"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Multidimensional Array of STRINGS [Re: EvilSOB] #397008
03/13/12 10:23
03/13/12 10:23
Joined: Mar 2011
Posts: 3,150
Budapest
sivan Offline
Expert
sivan  Offline
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
today I learnt a lot again...

finally I made a struct array containing pointers to the previously created 2d arrays, so now can handle my pathfinder system more easily, e.g. by using the struct with a proper index as function call parameter ensuring easy access to those arrays by simultaneously running subroutines. and now memory allocation can be hold on minimal level. fine.


Free world editor for 3D Gamestudio: MapBuilder Editor
Re: Multidimensional Array of STRINGS [Re: sivan] #397010
03/13/12 11:52
03/13/12 11:52
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Glad to be of service.


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Multidimensional Array of STRINGS [Re: EvilSOB] #407149
09/07/12 12:48
09/07/12 12:48
Joined: Sep 2012
Posts: 74
Niedersachsen, Germany
P
PriNova Offline
Junior Member
PriNova  Offline
Junior Member
P

Joined: Sep 2012
Posts: 74
Niedersachsen, Germany
@EvilSOB:

i need to use also multiarrays and tried your example like in post #2, but it didn't worked.

is there a good documentation about multiarrays in c-lite. i searched the forum and found nothing what works.

thanks

btw: i code some genetic algorithm-module and share if it will work

Re: Multidimensional Array of STRINGS [Re: PriNova] #407155
09/07/12 14:40
09/07/12 14:40
Joined: Sep 2012
Posts: 74
Niedersachsen, Germany
P
PriNova Offline
Junior Member
PriNova  Offline
Junior Member
P

Joined: Sep 2012
Posts: 74
Niedersachsen, Germany
i only get it to work if i define the string array with pre-defined dimensions like:

STRING** mystring[5][5]

beccause in this example i know the dimensions already, but how to do with dynamic arrays i don't know.

Last edited by PriNova; 09/07/12 14:42.
Page 1 of 3 1 2 3

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