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