home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / sys / mac / programm / 22048 < prev    next >
Encoding:
Text File  |  1993-01-24  |  3.7 KB  |  101 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!usc!sol.ctr.columbia.edu!The-Star.honeywell.com!umn.edu!csus.edu!netcom.com!cokenias
  3. From: cokenias@netcom.com (Damon Cokenias)
  4. Subject: Re: How to change an int to a string in Think C?
  5. Message-ID: <1993Jan24.032308.15071@netcom.com>
  6. Organization: Netcom - Online Communication Services (408 241-9760 guest)
  7. References: <1993Jan23.191336.877@dunix.drake.edu>
  8. Date: Sun, 24 Jan 1993 03:23:08 GMT
  9. Lines: 90
  10.  
  11. In article <1993Jan23.191336.877@dunix.drake.edu> kjt001@dunix.drake.edu (Devil Bunny!) writes:
  12.  
  13. >I'm working with Think C 4.0, and I need to display a number in a dialog
  14. >with ParamText, but I don't know how to do it.  If I use the following:
  15. >
  16. >ParamText("\p"+number,"","","");
  17. >
  18.  
  19. This looks more like C++ or Hypertalk rather than C.  Unfortunately for you,
  20. a C compiler won't even hesitate to compile this line.  What you are doing is
  21. taking a pointer to an empty Pascal string and adding 'number' bytes to the
  22. address.  Then you're passing it to ParamText which is getting a pointer to
  23. some random place in memory.
  24.  
  25.  
  26. >It gives me garbage instead of a number.  So I figure I need to change
  27. >the number to a text string first.  I tried using sprintf to do this,
  28. >but it seems to work incorrectly (gives me the wrong number) the first
  29. >time, and then later, it gives me 'odd address' error when the following
  30. >line is reached:
  31. >
  32. >    thePart = FindWindow(gTheEvent.where, &whichWindow);
  33. >    switch(thePart)
  34.  
  35. These two lines are probably not the ones giving you the greif.  You probably
  36. previously trashed all sorts of memory structures and these commands were the
  37. straws that broke the camel's back.
  38.  
  39. >The sprintf line is written as follows:
  40. >
  41. >        timeOfClick = (thisclick-lastclick);
  42. >        sprintf(textLine,"\p%d",timeOfClick);
  43. >        ParamText(textLine,"","","");
  44. >with the following definitions:
  45. >
  46. >    char        *textLine;
  47. >long int    lastclick,thisclick,timeOfClick;
  48.  
  49. Whoa, several things going on here.  First of all, the pointer 'textLine' has
  50. to be pointing to an array of characters if you want to store information
  51. in it.  Right now I imagine that it is just dangling-- sometimes pointing to
  52. free memory, sometimes pointing to somewhere important.  When sprintf stores
  53. data there, it can trash things that the memory manager, etc. rely on.  (That
  54. explains the above bug.).
  55.  
  56. Secondly, you are passing a Pascal string to sprintf.  I suppose you do this
  57. with the hope that it will give you one back.  It won't. Rather, the fact that
  58. it is a pascal string will probably just confuse it.
  59.  
  60. Finally, since timeOfClick is a long integer, you need to change %d to %ld.
  61. That's the letter 'ell' for long.
  62.  
  63. This is how your code should look:
  64.  
  65. char         textLine [100];        /* this reserves 100 characters */
  66. long int    lastclick, thisclick, timeOfClick;
  67.  
  68. ...
  69. timeOfClick = (thisclick-lastclick);
  70. sprintf(textLine,"%ld",timeOfClick); /* its a C string and uses long ints */
  71.  
  72. CtoPstr (textLine);    /* convert the C string to a pascal string */
  73.  
  74. ParamText(textLine,"","","");
  75.  
  76. >
  77. >Can anyone help me?  Is there a better way to convert a number to text
  78. >than sprintf?  Or don't I need to?
  79. >
  80.  
  81. Yes, there is a better way.  See Inside Mac volume I page 489 for a description
  82. of NumToString.  It takes a long int and a pointer to a string to store the
  83. result in.  The result will be a pascal string.
  84.  
  85. >
  86. >Thanks for any help!
  87. >
  88.  
  89. Glad to do it.  Seems to me you are moving to C from another language.  There
  90. are several good books to make this easier.  C is not just Pascal with different
  91. syntax.  If you don't have a copy of The C Programming Language by Kernighan
  92. and Ritchie, go and buy it tonight.  It's some of the dryest reading but you
  93. can bet that 90% of all professional C programmers have a well thumbed copy
  94. lying around.  Get the second eddition, by the way.
  95.  
  96. Lots of luck!
  97.  
  98. -Damon Cokenias
  99.  
  100.  
  101.