jeff@unh.UUCP (Jeffrey E. F. Friedl) (02/22/89)
Here's a cute one. I had a user come to me asking about fread/fwrite, showing me the man page: standard fread() man page "SYNTAX" section: fread(ptr, sizeof(*ptr), nitems, stream) char *ptr; unsigned nitems, sizeof(*ptr) FILE *stream; Pointing to "sizeof(*ptr)" then to "char *ptr", he asked "but won't that always be one?" I never noticed the "error" -- sort of cute, eh? I can just imagine a novice user sitting with the man page and pounding at the program wondering why it doesn't work! I offered that the following might be better: ---------------vvvvvvv fread((char*)ptr, sizeof(*ptr), nitems, stream) item *ptr; unsigned nitems, sizeof(*ptr) ---------^^^^ FILE *stream; which seems to make things more clear. I left it at that. I suppose I should tell him that sizeof(char) is !always 1, though. *jeff* ------------------------------------------------------------------------------ Jeffrey Eric Francis Friedl Box 2173 Babcock House ..!{uunet,decvax}!unh!jeff Durham New Hampshire 03824 Ever worked in Japan? Let me know your "what-to-do, what-never-to-do" stories. I'm moving there in May (or so -- whenver the Visa comes through).
mouse@mcgill-vision.UUCP (der Mouse) (03/07/89)
In article <939@unh.UUCP>, jeff@unh.UUCP (Jeffrey E. F. Friedl) writes: > I suppose I should tell him that sizeof(char) is !always 1, though. It's not? The Bible, second edition, A7.4.8 (page 204), third sentence: When sizeof is applied to a char, the result is 1; [...]. I think the first edition contained something similar, though I'm not sure. Do you know of any compilers which disagree? der Mouse old: mcgill-vision!mouse new: mouse@larry.mcrcim.mcgill.edu
gwyn@smoke.BRL.MIL (Doug Gwyn ) (03/08/89)
In article <1463@mcgill-vision.UUCP> mouse@mcgill-vision.UUCP (der Mouse) writes: >In article <939@unh.UUCP>, jeff@unh.UUCP (Jeffrey E. F. Friedl) writes: >> I suppose I should tell him that sizeof(char) is !always 1, though. > When sizeof is applied to a char, the result is 1; [...]. >I think the first edition contained something similar, though I'm not >sure. Do you know of any compilers which disagree? K&R 1st Ed. Appendix A, generally taken as the de facto pre-ANSI C standard, says that the units of sizeof are bytes, undefined by the C language. It also notes that in all existing (meaning AT&T) C implementations byte and char are essentially synonymous. The proposed ANSI C standard officially requires the identity between the byte (sizeof unit) and the space used to hold a char. There is apparently a LOT of existing code that relied on that non-guaranteed property, so X3J11 decided it would be a useful service to guarantee it. (I argued against that, for reasons posted to this newsgroup some time ago.)