Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!rutgers!labrea!aurora!ames!ptsfa!ihnp4!inuxc!iuvax!bsu-cs!dhesi From: dhesi@bsu-cs.UUCP (Rahul Dhesi) Newsgroups: comp.unix.xenix,comp.lang.c Subject: Passing (char *) NULL to printf to match %s Message-ID: <875@bsu-cs.UUCP> Date: Fri, 24-Jul-87 13:22:46 EDT Article-I.D.: bsu-cs.875 Posted: Fri Jul 24 13:22:46 1987 Date-Received: Sat, 25-Jul-87 17:51:44 EDT References: <166@qetzal.UUCP> <157@hobbes.UUCP> Reply-To: dhesi@bsu-cs.UUCP (Rahul Dhesi) Followup-To: comp.lang.c Organization: CS Dept, Ball St U, Muncie, Indiana Lines: 35 Summary: Probably legal according to ANSI, but a bad idea Xref: mnetor comp.unix.xenix:513 comp.lang.c:3275 In article <157@hobbes.UUCP> root@hobbes.UUCP (John Plocher) writes: >+---- (Robert White) writes the following in article <166@qetzal.UUCP> ---- >| 10. Passing (char *) NULL to a function in a large model program can cause >| a core dump/program abort. . . . >| This blows up a LOT of things, including printf("%s", (char *) NULL) >| (or equivalent). >+---- > > WRONG! Passing the NULL pointer is not what is wrong! What IS wrong > is the ASSUMPTION that the data stored at location NULL is anything useful! > > printf("%s", (char *) NULL); says print the string at address 0. This > happens to work on a VAX because that machine SPECIFICALLY has set things > up so that the contents of location 0 is 0. This bad coding practice > hits owners of Sun machines as well and is NOT a compiler bug. I believe that the interpretation of (char *) NULL when supplied as the actual parameter where printf is looking for a string may have changed over the years. The "correct" behavior today, according to ANSI C as I know it, is for printf to print a token signifying that a NULL pointer was passed. Microsoft C will print the string "(null)" when this happens. However my System V Release 2 manual as supplied with Microport System V/AT says that printf's behavior on a NULL pointer is undefined. (Then again, the UNIX C compilers are quite far away from ANSI C, since they don't support function prototypes at all.) I agree that passing NULL to printf is a bad idea, mostly because it's not wise to assume that any C compiler conforms to ANSI specs, especially since ANSI specs still aren't official. I added comp.lang.c to the newsgroups heading, so the Real Gurus can give us their opinion. Let's keep followups there or, better still, let the Gurus have the last word. -- Rahul Dhesi UUCP: {ihnp4,seismo}!{iuvax,pur-ee}!bsu-cs!dhesi