NXC: FlattenVar issue?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: FlattenVar issue?

Post by HaWe »

hey,
if I pass a string (!) to FlattenVar //ok, it's maybe odd, but let's follow the white rabbit....
then the string length is 1 more than originally.

e.g,
s=FlattenVar("world");
l=strlen(s); // says : 6!
if I write it on the screen, then there indeed is an additional blank behind:

Code: Select all

TextOut(0,0,s+"<"); // says: world < , with a blank in between.
more than this, if I concat strings together like
s=FlattenVar("world");
s2=StrCat("hello ",s,"!");

Code: Select all

TextOut(0,0,s2); // says: hello world                     (...lots of blanks...)        ...          without the final "!"
On the other hand, if I insert a strncpy:
strncpy(s,s,strlen(s)-1); // shorten string by 1 and terminate
s2=StrCat("hello ",s,"!");

Code: Select all

TextOut(0,0,s2); // says correctly: hello world!       (with final "!" and without any white space)
It seems there is no final \0 string termination and/or it's filled up with white space or something? If that's correct, does one have to terminate that by oneself like

s=FlattenVar("world")+"\0";

- or do I miss something else?
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: FlattenVar issue?

Post by HaWe »

John,
is it a bug or not?
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: FlattenVar issue?

Post by afanofosc »

The output of FlattenVar should not be considered a string in the same sense as a variable of type string or other functions which process strings. Whatever you pass into FlattenVar will be flattened into a byte array with a null appended to the end, i.e,. a byte which has the value 0. The strlen function is implemented by the compiler as ArrayLen-1 so it doesn't check for nulls and it is not built into the firmware. If you want to FlattenVar a string you should first StrToByteArray it, which will remove the null before FlattenVar puts it back. Generally, you can't expect that what you get out of FlattenVar is safe to use in all cases like it was a typical string since depending on what you flatten there will be embedded nulls that can lead to trouble with other string API functions.

Why would you want to do string manipulations to the result of FlattenVar?

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: FlattenVar issue?

Post by HaWe »

I came upon this issue during workarounds trying to pass a "%s" fmtstr to FormatNum.
I had to assign a variable of any kind (nums and/or strings) to a string variable.
Now that this special issue is resolved by the latest BCC version it's of no longer actual interest, but I was curious why I had those strange results at that time.

I always thought a byte array terminated by \0 is just (identically) the same as a string , and I considered strlen to count the number of chars (bytes) until \0 once appears in the byte array (like it's in C).
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest