Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Radiant on Tue 16/03/2004 13:12:01

Title: Uninitalized strings
Post by: Radiant on Tue 16/03/2004 13:12:01
This is not really a problem but I was curious as to what happened.


struct foo {
 string desc;
};
foo bar [5];


Now whenever I refer to 'StrLen (bar[0].desc)', the game crashes with an obscure error message. However if, at some earlier point, I set 'bar[0].desc = ""; ', there is no problem.
It seems to work with regular strings though, is this an AGS bug?
Title: Re:Uninitalized strings
Post by: SSH on Tue 16/03/2004 13:20:33
strings in structs have to be declared as

char desc [256];

Title: Re:Uninitalized strings
Post by: Radiant on Wed 17/03/2004 12:24:03
Strangely enough, my code works without any problems if I declare the string as a string. Whereas if I declare it as a character array, I cannot seem to initialize it with 'foo = "hello world" '.
Title: Re:Uninitalized strings
Post by: SSH on Wed 17/03/2004 13:23:46
It might work in some circumstances, but might crash in others.
That's because "string" is actually a pointer underneath. So you set your pointer to point at a constant string. This is not safe, especially if you try and chnage the string at a later point.
You shoudl use StrCopy(bar.desc, "hello world") instead.