An interesting behaviour in printf
Andrew Koenig
ark at alice.UUCP
Sat Mar 18 03:57:20 AEST 1989
In article <960 at Portia.Stanford.EDU>, joe at hanauma (Joe Dellinger) writes:
> What would you expect the following program to print out?
> #include <stdio.h>
> main()
> {
> char string[10];
> string[0] = '*';
> string[1] = '\0';
> printf("%s\n", string[1]);
> }
The program is invalid; the implementation is allowed to
do as it pleases.
You said
string[1] = '\0';
which is equivalent to
string[1] = 0;
and then said
printf("%s\n", string[1]);
which is, of course, equivalent to
printf("%s\n", 0);
This asks printf to print characters starting at memory location 0;
not quite what you probably wanted.
Try this:
printf("%s\n", &string[1]);
or, equivalently, this:
printf("%s\n", string+1);
--
--Andrew Koenig
ark at europa.att.com
More information about the Comp.lang.c
mailing list