I think you'll find you actually need to allocate some memory to copy the
string into. You've defined tempText2 as a CharPtr...
> I've written a chunk of code that displays some text on the screen. I've
> declared a gadget where the text will be displayed. However, whenever the
> text is copied using StrCopy, I'll always get a "bus error". Can someone
> help me?
>
> static void PrintOnScreen(const CharPtr text)
> {
> Int16 lineHeight;
> Int16 strLength;
> Int16 gadgetWidth;
> Boolean canFit;
> Word objIndex;
> RectangleType bounds;
> CharPtr tempText1;
> CharPtr tempText2;
> FormPtr frm;
.. and then this next statement sets the pointer to the address of "" which is
a NULL string the compiler will have automatically allocated somewhere for you
(not quite sure where...data segment for the application?) which you're then
trying to copy the string into, essentially tromping around in memory you
shouldn't be.
To summarise this is a common problem when you're learning to use pointers to
things...CharPtr points to a character string, but where does the character
string live? Not where it should in your code (sorry, not meaning to be
condescending here...it's stuff we've all burned ourselves on in the past ;)
I'm sure!).
> tempText2 = "";
>
> *********** Problem lies here ***********
> StrCopy (tempText1, text);
> StrNCopy (tempText2, text, strLength); // copy string that fits
> *********** Problem lies here ***********
I think something like
Char tempText1[ LENGTH_OF_BIGGEST_STRING_I_EXPECT + 1 ];
Char tempText2[ LENGTH_OF_BIGGEST_STRING_I_EXPECT + 1 ];
..
StrCopy (tempText1, text);
StrNCopy( tempText2, text, strLength );
would be a better idea. Or if you don't know how big your biggest string will
be until you actually run the app, you can allocate the memory dynamically
yourself using MemPtrNew (or better yet, MemHandleNew).
Hope that helps?
Stuart Nicholson
http://homepages.ihug.co.nz/~snic/palm/index.html
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/