home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.mac.programmer
- Path: sparky!uunet!usc!sol.ctr.columbia.edu!The-Star.honeywell.com!umn.edu!csus.edu!netcom.com!cokenias
- From: cokenias@netcom.com (Damon Cokenias)
- Subject: Re: How to change an int to a string in Think C?
- Message-ID: <1993Jan24.032308.15071@netcom.com>
- Organization: Netcom - Online Communication Services (408 241-9760 guest)
- References: <1993Jan23.191336.877@dunix.drake.edu>
- Date: Sun, 24 Jan 1993 03:23:08 GMT
- Lines: 90
-
- In article <1993Jan23.191336.877@dunix.drake.edu> kjt001@dunix.drake.edu (Devil Bunny!) writes:
-
- >I'm working with Think C 4.0, and I need to display a number in a dialog
- >with ParamText, but I don't know how to do it. If I use the following:
- >
- >ParamText("\p"+number,"","","");
- >
-
- This looks more like C++ or Hypertalk rather than C. Unfortunately for you,
- a C compiler won't even hesitate to compile this line. What you are doing is
- taking a pointer to an empty Pascal string and adding 'number' bytes to the
- address. Then you're passing it to ParamText which is getting a pointer to
- some random place in memory.
-
-
- >It gives me garbage instead of a number. So I figure I need to change
- >the number to a text string first. I tried using sprintf to do this,
- >but it seems to work incorrectly (gives me the wrong number) the first
- >time, and then later, it gives me 'odd address' error when the following
- >line is reached:
- >
- > thePart = FindWindow(gTheEvent.where, &whichWindow);
- > switch(thePart)
-
- These two lines are probably not the ones giving you the greif. You probably
- previously trashed all sorts of memory structures and these commands were the
- straws that broke the camel's back.
-
- >The sprintf line is written as follows:
- >
- > timeOfClick = (thisclick-lastclick);
- > sprintf(textLine,"\p%d",timeOfClick);
- > ParamText(textLine,"","","");
- >with the following definitions:
- >
- > char *textLine;
- >long int lastclick,thisclick,timeOfClick;
-
- Whoa, several things going on here. First of all, the pointer 'textLine' has
- to be pointing to an array of characters if you want to store information
- in it. Right now I imagine that it is just dangling-- sometimes pointing to
- free memory, sometimes pointing to somewhere important. When sprintf stores
- data there, it can trash things that the memory manager, etc. rely on. (That
- explains the above bug.).
-
- Secondly, you are passing a Pascal string to sprintf. I suppose you do this
- with the hope that it will give you one back. It won't. Rather, the fact that
- it is a pascal string will probably just confuse it.
-
- Finally, since timeOfClick is a long integer, you need to change %d to %ld.
- That's the letter 'ell' for long.
-
- This is how your code should look:
-
- char textLine [100]; /* this reserves 100 characters */
- long int lastclick, thisclick, timeOfClick;
-
- ...
- timeOfClick = (thisclick-lastclick);
- sprintf(textLine,"%ld",timeOfClick); /* its a C string and uses long ints */
-
- CtoPstr (textLine); /* convert the C string to a pascal string */
-
- ParamText(textLine,"","","");
-
- >
- >Can anyone help me? Is there a better way to convert a number to text
- >than sprintf? Or don't I need to?
- >
-
- Yes, there is a better way. See Inside Mac volume I page 489 for a description
- of NumToString. It takes a long int and a pointer to a string to store the
- result in. The result will be a pascal string.
-
- >
- >Thanks for any help!
- >
-
- Glad to do it. Seems to me you are moving to C from another language. There
- are several good books to make this easier. C is not just Pascal with different
- syntax. If you don't have a copy of The C Programming Language by Kernighan
- and Ritchie, go and buy it tonight. It's some of the dryest reading but you
- can bet that 90% of all professional C programmers have a well thumbed copy
- lying around. Get the second eddition, by the way.
-
- Lots of luck!
-
- -Damon Cokenias
-
-
-