home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / sys / mac / programm / 22051 < prev    next >
Encoding:
Internet Message Format  |  1993-01-24  |  4.3 KB

  1. Path: sparky!uunet!olivea!pagesat!spssig.spss.com!uchinews!ellis!chh9
  2. From: chh9@ellis.uchicago.edu (Conrad Halling)
  3. Newsgroups: comp.sys.mac.programmer
  4. Subject: Re: How to change an int to a string in Think C?
  5. Message-ID: <1993Jan23.221540.16242@midway.uchicago.edu>
  6. Date: 23 Jan 93 22:15:40 GMT
  7. References: <1993Jan23.191336.877@dunix.drake.edu>
  8. Sender: news@uchinews.uchicago.edu (News System)
  9. Reply-To: chh9@midway.uchicago.edu
  10. Organization: University of Chicago
  11. Lines: 95
  12.  
  13. In article <1993Jan23.191336.877@dunix.drake.edu> 
  14.   kjt001@dunix.drake.edu (Devil Bunny!) writes:
  15.  
  16. >I'm working with Think C 4.0, and I need to display a number in a dialog
  17. >with ParamText, but I don't know how to do it.
  18.  
  19. [some lines omitted.]
  20.  
  21. >I figure I need to change
  22. >the number to a text string first.  I tried using sprintf to do this,
  23. >but it seems to work incorrectly (gives me the wrong number).
  24. >
  25. >The sprintf line is written as follows:
  26. >
  27. >        timeOfClick = (thisclick-lastclick);
  28. >        sprintf(textLine,"\p%d",timeOfClick);
  29. >        ParamText(textLine,"","","");
  30. >
  31. >with the following definitions:
  32. >
  33. >    char        *textLine;
  34. >long int    lastclick,thisclick,timeOfClick;
  35.  
  36. This doesn't work for the following reasons:
  37.  
  38. First of all, you have defined textLine as a pointer to a character, but
  39. you haven't actually pointed textLine at anything.  This means that the
  40. textLine variable will be filled with garbage and will point at a random
  41. location in memory.  When you call sprintf(), it will send its output
  42. to that random location, and who knows what you're writing over?
  43.  
  44. Second, you are giving sprintf() a Pascal string as one of its arguments.
  45. sprintf() expects a C string.  This will definitely mess things up, too.
  46. When you are using the debugger, you can check what's inside your strings
  47. and look at them as C strings or Pascal strings.  This is very handy.
  48.  
  49. You need to allocate memory for your string.  sprintf requires a pointer
  50. to a C string.  You then convert this to a Pascal string, which is then
  51. passed to ParamText().  Here is one way to do it:
  52.  
  53.     Str255      textLine;
  54.     long        lastClick,
  55.                 thisClick,
  56.                 timeOfClick;
  57.  
  58.     timeOfClick = thisClick - lastClick;
  59.     sprintf( ( char * ) textLine, "%ld", timeOfClick );
  60.     CtoPstr( ( char * ) textLine );
  61.     ParamText( textLine, "\p", "\p", "\p" );
  62.  
  63. Here are some subtleties:
  64.  
  65. A Str255 is an array of 256 unsigned chars.  When you say
  66.     Str255           textLine;
  67. what the compiler understands this to mean is:
  68.     unsigned char    textLine[256];
  69.  
  70. The length byte of a Pascal string is kept in the first byte of the array.  
  71. A standard C string is an array (of any length) of chars which is terminated 
  72. by 0x00.  You have to keep this in mind since the C ANSI functions will 
  73. use C strings, but the Macintosh toolbox expects Pascal strings.  This 
  74. means you have to convert them back and forth.
  75.  
  76. sprintf expects a pointer to an array of chars; textLine is this pointer.  
  77. You have to cast textLine so the compiler will know that you know what 
  78. you're doing.
  79.  
  80. When you want to printf a long int, you use "%ld" instead of "%d".
  81.  
  82. To convert your C string to a Pascal string, use CtoPstr().  (To reverse
  83. the conversion, use PtoCstr().  These functions are provided as a
  84. convenience by THINK C.  They are not in the ANSI standard.)  Since
  85. CtoPstr() expects a pointer to a C string (chars), you must cast your
  86. pointer (which is to an array of unsigned chars).
  87.  
  88. "\p" tells the compiler to build a constant string as a Pascal string, with
  89. the length byte in front.  If your Pascal string is "\p", the compiler
  90. converts this to a Pascal string consisting only of the length byte, which
  91. is set to zero.  ParamText() expects pointers to arrays of unsigned
  92. characters.  You supply "\p" for the empty strings. Inside Macintosh I-421 
  93. implies that in C you could pass NIL, which is a pointer to memory location
  94. 0, which is a convenient way in C of passing a pointer to nothing.
  95.  
  96. Understanding all this string stuff takes a lot of time.  You will think
  97. you understand it all, and then later you will be totally confused again.
  98. But keep working at it, and pretty soon it won't be a problem any more.
  99.  
  100. Please excuse any errors.  Knowing me, there probably is one in here
  101. somewhere.  Also, please understand that I'm used to working with THINK C
  102. 5.0; I understand THINK C 4.0 works slightly differently (so why aren't
  103. you using the new version??).
  104.  
  105. -- 
  106. Conrad Halling
  107. c-halling@uchicago.edu
  108.