Gamestudio Links
Zorro Links
Newest Posts
Newbie Questions
by fairtrader. 12/06/23 11:29
Zorro Trader GPT
by TipmyPip. 12/04/23 11:34
Square root rule
by Smallz. 12/02/23 09:15
RTest not found error
by TipmyPip. 12/01/23 21:43
neural function for Python to [Train]
by TipmyPip. 12/01/23 14:47
Xor Memory Problem.
by TipmyPip. 11/28/23 14:23
Training with command line parameters
by TipmyPip. 11/26/23 08:42
Combine USD & BTC Pairs In Asset Loop
by TipmyPip. 11/26/23 08:30
AUM Magazine
Latest Screens
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Who's Online Now
7 registered members (3run, miwok, AndrewAMD, Quad, TipmyPip, fairtrader, 1 invisible), 637 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
fairtrader, hus, Vurtis, Harry5, KelvinC
19019 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
array of strings #301189
12/08/09 20:49
12/08/09 20:49
Joined: Oct 2003
Posts: 4,131
M
Matt_Aufderheide Offline OP
Expert
Matt_Aufderheide  Offline OP
Expert
M

Joined: Oct 2003
Posts: 4,131
How do you properly use an array of strings and access them? I declare a string array like this:
STRING* mystring[20];

then try to access it like this:
TEXT* mytxt =
{string(mystring[0]);}

and i get an error...


Sphere Engine--the premier A6 graphics plugin.
Re: array of strings [Re: Matt_Aufderheide] #301196
12/08/09 21:48
12/08/09 21:48
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
By this line
STRING* mystring[20];
you create just an array of string POINTERS, not of actual strings. You have to create them with str_create like this:
var i;
for(i=0;i<20;i++) mystring[i] = str_create("blah");

Last edited by Lukas; 12/08/09 22:15. Reason: = was missing
Re: array of strings [Re: Matt_Aufderheide] #301197
12/08/09 21:50
12/08/09 21:50
Joined: Aug 2007
Posts: 1,922
Schweiz
Widi Offline
Serious User
Widi  Offline
Serious User

Joined: Aug 2007
Posts: 1,922
Schweiz
From the manual see under "text.strings":

TEXT* welcome_txt =
{
...
strings = 5; // 5 different STRINGs
}

To read or whrite to a string, use: " (text.pstring)[] ".
For example:
str_cpy((welcome_txt.pstring)[2],"This is my Text");

Re: array of strings [Re: Widi] #301200
12/08/09 22:25
12/08/09 22:25
Joined: Oct 2003
Posts: 4,131
M
Matt_Aufderheide Offline OP
Expert
Matt_Aufderheide  Offline OP
Expert
M

Joined: Oct 2003
Posts: 4,131
I don't want multiple strings per text, i want to use a member of a string array in a text.

but this doesn't even compile..i dont think it matters if I dont create the strings at run time, it should at least compile and then crash:
TEXT* mytxt =
{string(mystring[0]);}


Sphere Engine--the premier A6 graphics plugin.
Re: array of strings [Re: Matt_Aufderheide] #301201
12/08/09 22:41
12/08/09 22:41
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
it's not possible. you have to use a workaround.

STRING *mystring0 = mystring[0];
STRING *mystring1 = mystring[1];
...

use those in the text then.

Re: array of strings [Re: ventilator] #301371
12/09/09 23:29
12/09/09 23:29
Joined: Jul 2008
Posts: 1,178
England
M
MrGuest Offline
Serious User
MrGuest  Offline
Serious User
M

Joined: Jul 2008
Posts: 1,178
England
can't you just use a TEXT pointer for storing all the strings initially

Code:
TEXT* txt_names = {
	strings = 4;
	string("dave", "fred", "pete", "john");
}
TEXT* txt_display = {
	strings = 2;
	pos_x = 200;
	flags = SHOW;
}

...
	(txt_display.pstring)[0] = (txt_names.pstring)[x];
...


if i knew what you were trying to do initially with the strings, could possibly give a different approach

hope this helps

Re: array of strings [Re: MrGuest] #318211
04/05/10 23:35
04/05/10 23:35
Joined: Sep 2003
Posts: 303
Germany
Clemens Offline
Senior Member
Clemens  Offline
Senior Member

Joined: Sep 2003
Posts: 303
Germany
Lukas method looks like the most comfortable one!

But it only works when the string is defined inside the function.

Examples:
Code:
#include <acknex.h>
void main() {
	STRING* ArrayStr[3];
	ArrayStr[0] = str_create("1");
	ArrayStr[1] = str_create("2");
	ArrayStr[2] = str_create("3");
	while (1) {
		draw_text(ArrayStr[1], 10,10, vector(100,100,255));
		wait(1);
	}
}


Does work-> a red "2" appears on the screen!

Code:
#include <acknex.h>
STRING* ArrayStr[3];
ArrayStr[0] = str_create("1");
ArrayStr[1] = str_create("2");
ArrayStr[2] = str_create("3");
void main() {
	while (1) {
		draw_text(ArrayStr[1], 10,10, vector(100,100,255));
		wait(1);
	}
}


Doesn't work-> "Malfunction W1501 - Empty pointer in main"
Reason has to be that objects/structs created by the _create command are only exist in their definition area!?!

How to solve that problem?

Re: array of strings [Re: Clemens] #318212
04/06/10 00:16
04/06/10 00:16
Joined: Dec 2008
Posts: 271
Saturnus Offline
Member
Saturnus  Offline
Member

Joined: Dec 2008
Posts: 271
Function calls and variable assignments have to be inside a function. So you have to place the following lines within your main():
Code:
ArrayStr[0] = str_create("1");
ArrayStr[1] = str_create("2");
ArrayStr[2] = str_create("3");



Re: array of strings [Re: Saturnus] #318248
04/06/10 12:03
04/06/10 12:03
Joined: Sep 2003
Posts: 303
Germany
Clemens Offline
Senior Member
Clemens  Offline
Senior Member

Joined: Sep 2003
Posts: 303
Germany
Hmm, but variablen haven't to be assigned inside a function. If you want them global you "need" them outside.
But why doesn't this work as well with string arrays? Cause I need a global string array!

Re: array of strings [Re: Clemens] #318259
04/06/10 13:32
04/06/10 13:32
Joined: Dec 2008
Posts: 271
Saturnus Offline
Member
Saturnus  Offline
Member

Joined: Dec 2008
Posts: 271
As said variable assignments have to be inside a function. Variable initializations are the only exception (int number = 2;).

However, your variable declarations can almost be wherever you want. So you can declare "STRING* ArrayStr[3]" globally and have the assignments (ArrayStr[0] = ...) inside your main function (or inside an arbitrary other function). THen everything will be fine!

Page 1 of 2 1 2

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