Uninitalized strings

Started by Radiant, Tue 16/03/2004 13:12:01

Previous topic - Next topic

Radiant

This is not really a problem but I was curious as to what happened.

Code: ags

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?

SSH

strings in structs have to be declared as
char desc [256];

12

Radiant

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" '.

SSH

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.
12

SMF spam blocked by CleanTalk