[Not a bug] Weird string array behaviour

Bugs that have been investigated and resolved somehow.

Moderator: GZDoom Developers

Forum rules
Please don't bump threads here if you have a problem - it will often be forgotten about if you do. Instead, make a new thread here.
boris
Posts: 702
Joined: Tue Jul 15, 2003 3:37 pm

Weird string array behaviour

Post by boris »

Hi!

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

Code: Select all

#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?
User avatar
Chris
Posts: 2845
Joined: Thu Jul 17, 2003 12:07 am
Graphics Processor: ATI/AMD with Vulkan Support

Post by Chris »

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.
boris
Posts: 702
Joined: Tue Jul 15, 2003 3:37 pm

Post by boris »

Ah, seems pretty logical :) Still a bit uncomfortable though. Especially when you got several string arrays.
User avatar
randi
Site Admin
Posts: 7728
Joined: Wed Jul 09, 2003 10:30 pm

Post by randi »

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 all

#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 all

str AnEmptyString = "";
in zcommon.acs if you want.
boris
Posts: 702
Joined: Tue Jul 15, 2003 3:37 pm

Post by boris »

Yay, works nice!

Return to “Closed Bugs”