FlattenVAR a struct with a string in it

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: FlattenVAR a struct with a string in it

Post by mcsummation »

John, I spent years writing code and my own documentation. My attitude was, "If one of my users has a problem using my software, then either the code is broken or the documentation is and I have to fix whichever is wrong."
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: FlattenVAR a struct with a string in it

Post by mattallen37 »

The link John pointed you to seems to pretty well sum up the documentation you think is missing.

I know you're not programming directly in NBC, but NXC is built on it, so you really are using it (with all it's features/limits).
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: FlattenVAR a struct with a string in it

Post by mcsummation »

In an attempt to bring this thread to a positive conclusion, here are my thoughts:
1) Strings are, by definition, variable in length. At one point in a program, a given string might be 1 character long, at another point it might be 50. (Notice that I used the term "character" and not byte. That's because we, as the programmer, don't really need to know how many bits contains a character. On the NXT it is 8 bits (byte). In other systems it might be 6 bits or 16 bits.)
2) There is nothing wrong about putting a string in a structure (struct). The implementation details will handle this just fine.
3) However, attempting to flatten/unflatten a structure containing a string can lead to bad things. John, in his post here, gave an explanation about how to address this issue.
4) To make life easier on us KISS* programmers, the size of the structure should be calculable at compile time. So, don't use a "string", use a "char array of fixed length", that way you and unflatten know what goes where and the structure will flatten/unflatten just fine.

* KISS = Keep It Simple Stupid. There are various words put in for the last one, but the first time I heard KISS (many years ago), Stupid was the word used. It it not politically correct now... That being said, many times my code is complex, but in trying to adhere to KISS, don't make things any harder than they have to be.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests