This is a basic C question, not specific to the Palm. When you declare a
variable to be of type CharPtr, you're only allocating memory for the
pointer, not for the string itself. If you want to allocate a fixed amount
of space for the string right on the stack, you can declare it as Char
dispLength[80] instead (where 80 is the number of bytes allocated, meaning
you have room for 79 characters plus the null terminator). Of course, the
number 80 is just an example - use as many bytes as necessary to fit the
longest string that can possibly be stored there, plus a byte for the null
terminator.
You can also allocate memory on the dynamic heap for the string. In this
case, your declaration would be as you have it, but you'd need to do
something like dispLength = MemPtrNew(80). To avoid a memory leak, you'd
need to free the memory as well, using MemPtrFree(dispLength). For your
purposes, I'd stick with stack allocation.
When you write something like dispLength = "", you're creating what's called
a string literal. This is a constant string. In this case, it's only one
byte for the null terminator, but it's still a string literal. It's
generally a bad idea to write over such strings, since the compiler may
optimize your code by making all references to the empty string literal
refer to the same constant string. So, if you overwrite it one place, a
later assignment of the same form may not work as expected.
More importantly, in this case you're writing past the end of this string
literal. Only one byte would be allocated for this constant string, and who
knows what's after it in memory. StrIToA will write the digits of the
integer you pass it to the address you specify. It has no idea how much
space is available there. It's up to you to make sure you've got enough
space.
--
Peter Epstein
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/