home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sys / mac / programm / 21216 < prev    next >
Encoding:
Internet Message Format  |  1993-01-11  |  3.2 KB

  1. Path: sparky!uunet!gatech!destroyer!news.itd.umich.edu!potts
  2. From: potts@oit.itd.umich.edu (Paul Potts)
  3. Newsgroups: comp.sys.mac.programmer
  4. Subject: Re: Think C bug? [sprintf]
  5. Date: 11 Jan 1993 22:14:32 GMT
  6. Organization: Instructional Technology Laboratory, University of Michigan
  7. Lines: 52
  8. Message-ID: <1isrg8INN5i3@terminator.rs.itd.umich.edu>
  9. References: <1993Jan10.225559.1549@midway.uchicago.edu> <kevin.726748544@crash.cts.com> <1993Jan11.194525.18175@afterlife.ncsc.mil>
  10. NNTP-Posting-Host: helen.oit.itd.umich.edu
  11.  
  12. In article <1993Jan11.194525.18175@afterlife.ncsc.mil> mssmith@afterlife.ncsc.mil (M. Scott Smith) writes:
  13. >Hi..  I'm the original poster who asked about potential problems with sprintf.
  14. >
  15. >It sounds like some people have had no problems with sprintf, while others
  16. >have.  I wasn't sure if it was commonly used by Mac C programmers; are there
  17. >Toolbox calls (non-ANSI) that Mac pros tend to use instead?
  18.  
  19. It depends on what you want to do. Basically, sprintf gives you the
  20. functionality of printf (formatted printing of strings, characters, etc.)
  21. to memory instead of to a device. Like printf, sprintf is not type-safe;
  22. it doesn't know about the types of things that you send it. The
  23. routine will try its best to do what you tell it, but if you give it, for
  24. example, a short quantity to print when you've told it via the formatting
  25. information to expect a longword, the results will be, as they say,
  26. "undefined."
  27.  
  28. I've used sprintf pretty extensively in XCMDs. I'll mention a few details
  29. to the newsgroup; if you want help on why some specific piece of code doesn't 
  30. seem to work, I'd be happy to take a look at it by mail. One of the nice things
  31. about sprintf is that it doesn't require the full ANSI library; you can
  32. use ANSI-small or ANSI-A4 for code resources. 
  33.  
  34. >
  35. >Does sprintf only operate with arrays of characters, or will it be happy with
  36. >types like Str255?  Or do I have to coerce Str255's to make them work?  
  37.  
  38. Like most of the string-handling functions in C, sprintf uses the generic
  39. type char * (pointer to character) as its argument, and this is what gets
  40. written to. If you're using sprintf you must make sure that your destination
  41. is what you expect it to be. If you're using the type Str255, remember that
  42. sprintf will write strings in the zero-terminated format, whereas Pascal code
  43. and a lot of the Toolbox functions expect strings in the length-first format.
  44. THINK C has a quickie function which will do this conversion.
  45.  
  46. >think some of my problems were traced back to that.  At the same time, I wasn't
  47. >sure what the problem was, because something would compile fine one way but
  48. >not the next.  (It seems if Think C saw me passing a Str255 to sprintf it
  49. >was happy about it; but if later in the program I passed a "real" string to
  50. >it, then it suddenly saw problems with one of the calls..  
  51.  
  52. Again, post an example...
  53.  
  54. >Is sprintf the best way to capture the functionality of Pascal's concat?
  55.  
  56. For this you might want to look into using the C function strcat, which
  57. will append one string on to another. Keep in mind that C strings can be
  58. any length, but if you expect to convert the string back to a Pascal-form
  59. string you must be sure that it will fit.
  60.  
  61. -- 
  62. Politics is crime pursued by other means.              
  63. potts@oit.itd.umich.edu      CI$ 71561,3362 (rarely)
  64.