Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Sslaxx on Wed 26/10/2011 14:20:04

Title: Strings, string replacing, chars and null pointers... help?
Post by: Sslaxx on Wed 26/10/2011 14:20:04
OK, related to http://www.adventuregamestudio.co.uk/yabb/index.php?topic=44702.msg597577 I decided to code up a function to remove illegal characters in filenames in strings (and to replace spaces with underscores). So far, this is what I have:

String StripChars (String ToStrip)
{
String StrippedTo;
char StripLook = 0;
int Count = 0;
while (Count < ToStrip.Length)
{
  StripLook = ToStrip.Chars [Count];  // Get the char at the current position in the string to strip.
  if (StripLook == ' ')
  {
   StripLook = '_';
  }
  StrippedTo = StrippedTo.AppendChar (StripLook);
  Count++;
}
return (StrippedTo);
}


Except it doesn't work. In the function, on the line that should append the character to the string that is to be returned as a filename-safe version, it's giving a "Null pointer referenced" error. I have a suspicion this might be due to the use of a char in there. But can anyone else help out here?

Thanks.
Title: Re: Strings, string replacing, chars and null pointers... help?
Post by: Calin Leafshade on Wed 26/10/2011 14:27:02
You need to initalise StrippedTo

line 3:

String StrippedTo = "";
Title: Re: Strings, string replacing, chars and null pointers... help?
Post by: Sslaxx on Wed 26/10/2011 14:30:54
Quote from: Calin Leafshade on Wed 26/10/2011 14:27:02
You need to initalise StrippedTo

line 3:

String StrippedTo = "";
Yup, figured it out just a minute ago!
Title: Re: Strings, string replacing, chars and null pointers... help?
Post by: monkey0506 on Wed 26/10/2011 23:12:58
That's something to keep in mind with the new-style String type is that although you don't use normal pointer nomenclature (ie., no asterisk), it is internally still a pointer, and pointer logic still (very much!) applies.

That does actually make it possible to make String parameters to functions optional, so it certainly has benefits. ;)