[Not a bug] Weird string array behaviour

Bugs that have been investigated and resolved somehow.

Moderator: GZDoom Developers

Weird string array behaviour

Postby boris » Mon Nov 03, 2003 2:15 pm

Hi!

I found some od string array behaviour. Try this little script:

Code: Select allExpand view
#include "zcommon.acs"

str bla[5];

script 1 open
{
   bla[0] = "heh";

   print(s:bla[0], s:"\n", s:bla[1], s:"\n", s:bla[2], s:"\n", s:bla[3], s:"\n", s:bla[4]);
}


It will display "heh" 5 times, i.e. all fields of the array get filled with "heh", and not only the very first one. Shouldn't they be set to "" or NULL or something by default?
boris
I post less than Manc and Hobo
 
Joined: 15 Jul 2003

Postby Chris » Mon Nov 03, 2003 2:25 pm

Strings are referenced by index in ACS, not pointers like in C. When you declare a string, it's set to 0, meaning index 0(1st of 256 strings), until assigned something else. So when you set bla[0] to "heh"(the first string, index 0), all strings that reference index 0, will display "heh", until you change them.
User avatar
Chris
 
Joined: 17 Jul 2003

Postby boris » Mon Nov 03, 2003 2:35 pm

Ah, seems pretty logical :) Still a bit uncomfortable though. Especially when you got several string arrays.
boris
I post less than Manc and Hobo
 
Joined: 15 Jul 2003

Postby randi » Mon Nov 03, 2003 2:51 pm

Just make the first string you use in your script "", and then your arrays will be initialized to "". It could be as simple as this, if you like:
Code: Select allExpand view
#include "zcommon.acs"
str AnEmptyString = "";

str bla[5];

script 1 open
{
   bla[0] = "heh";

   print(s:bla[0], s:"\n", s:bla[1], s:"\n", s:bla[2], s:"\n", s:bla[3], s:"\n", s:bla[4]);
}


You could even stick
Code: Select allExpand view
str AnEmptyString = "";
in zcommon.acs if you want.
User avatar
randi
Site Admin
 
Joined: 09 Jul 2003

Postby boris » Mon Nov 03, 2003 2:59 pm

Yay, works nice!
boris
I post less than Manc and Hobo
 
Joined: 15 Jul 2003


Return to Closed Bugs

Who is online

Users browsing this forum: No registered users and 0 guests