drw@cullvax.UUCP (Dale Worley) (07/29/87)
But, suppose we write: printf("%.0s", (char *)NULL) This should print zero characters out of the string, and so won't actually reference any character through the null pointer. Presumably this is valid ANSI C! Dale -- Dale Worley Cullinet Software ARPA: cullvax!drw@eddie.mit.edu UUCP: ...!seismo!harvard!mit-eddie!cullvax!drw From the Temple of St. Cathode of Vidicon:
gwyn@brl-smoke.ARPA (Doug Gwyn ) (07/30/87)
In article <1407@cullvax.UUCP> drw@cullvax.UUCP (Dale Worley) writes: > printf("%.0s", (char *)NULL) >This should print zero characters out of the string, and so won't >actually reference any character through the null pointer. Presumably >this is valid ANSI C! No, why should it be? The printf() implementation may very well always inspect the argument string to find its null terminator, for example to determine its length before converting it.
drw@cullvax.UUCP (Dale Worley) (07/31/87)
gwyn@brl-smoke.ARPA (Doug Gwyn ) writes: | In article <1407@cullvax.UUCP> drw@cullvax.UUCP (Dale Worley) writes: | > printf("%.0s", (char *)NULL) | >This should print zero characters out of the string, and so won't | >actually reference any character through the null pointer. Presumably | >this is valid ANSI C! | | No, why should it be? The printf() implementation may very well | always inspect the argument string to find its null terminator, | for example to determine its length before converting it. Eh? Well, certainly, if you do printf("%.100s", ...), you want it to work if the pointer doesn't point to a block of memory with a null byte in it? This is useful if you have a "string" stored in pointer/length format: printf("%.*s", len, ptr). Although I think that a careful reading of the ANSI document (p. 117, l. 26: "s format code: The argument shall be a pointer to a string." p. 84, l. 7: "A string is an array of characters terminated by a null character.") shows that I'm wrong. But this use of %s is useful and probably works in all implementations, and should be allowed. Dale -- Dale Worley Cullinet Software ARPA: cullvax!drw@eddie.mit.edu UUCP: ...!seismo!harvard!mit-eddie!cullvax!drw From the Temple of St. Cathode of Vidicon:
karl@haddock.ISC.COM (Karl Heuer) (08/01/87)
In article <1407@cullvax.UUCP> drw@cullvax.UUCP (Dale Worley) writes: >But, suppose we write: > printf("%.0s", (char *)NULL) >This should print zero characters out of the string, and so won't >actually reference any character through the null pointer. I seem to remember noticing once that one implementation of strncpy would test for a terminating NUL *before* testing the counter, thus when no NUL was present it would fetch an extra byte. Completely harmless, unless you happened to be at the end of readable memory! It would be nice if this were guaranteed not to happen (in printf as well as in the strnXXX family). In particular I agree it should be legal to grab zero characters from a NULL pointer, e.g. the result of [ANSI] malloc(0). Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint