Auto-initialized empty TEXT strings lack char array

Posted By: Saturnus

Auto-initialized empty TEXT strings lack char array - 12/27/10 20:20

According to the manual page about the strings parameter of TEXT objects, "String pointers that are not defined through the string() parameter are initialized to empty strings" (http://www.conitec.net/beta/atext-strings.htm).

However, in contrast to ordinary empty strings (e.g. STRING *str = "";), such strings don't have a char array assigned initially (str->chars == 0).

This doesn't seem to be a problem in most cases.
When such a string gets copied to another string, though, a W1501 occurs.

Perhaps this could be changed?
I think it would be more convenient if auto-initialized empty TEXT strings already had a char array assigned as ordinary empty strings do.

Code:
#include <acknex.h>

TEXT* txt =	{
	strings = 2;
	string(""); // only define the first string
}

void main () {
	printf("[0]->chars = %p\n[1]->chars = %p",
		((txt->pstring)[0])->chars,
		((txt->pstring)[1])->chars);
	
	str_cpy((txt->pstring)[0], (txt->pstring)[1]); // W1501
}


Posted By: jcl

Re: Auto-initialized empty TEXT strings lack char array - 12/28/10 10:18

It's the other way around:

str_cpy((txt->pstring)[1], (txt->pstring)[0]);

http://manual.3dgamestudio.net/astr_cpy.htm
Posted By: Saturnus

Re: Auto-initialized empty TEXT strings lack char array - 12/28/10 15:53

Yes, it works this way because the first string is defined in the string() parameter, while the second isn't.

My point in this respect is: Shouldn't both strings behave the same, whether or not they are defined in string()?

According to the manual, strings that are not defined in string() are initialized to empty strings automatically. However, these empty strings behave slightly different than empty strings that are defined in string(), because the latter already have a char array allocated, while the former have not. I thought there shouldn't be such a difference. (It's just a minor thing, of course.)
Posted By: jcl

Re: Auto-initialized empty TEXT strings lack char array - 12/28/10 17:31

Yes, an empty string is different to the string "". An empty string is a string without content, while the string "" contains one byte.

I admit that this is a not intuitive difference, but we have to live with that, as it's the way character strings are defined in C. If I would change that for text objects, we'd get all sorts of compatibility problems.
© 2024 lite-C Forums