Path: utzoo!attcan!uunet!lll-winken!ames!pasteur!ucbvax!tut.cis.ohio-state.edu!rutgers!att!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c Subject: Re: An interesting behaviour in printf Message-ID: <9066@alice.UUCP> Date: 17 Mar 89 17:57:20 GMT References: <960@Portia.Stanford.EDU> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 45 In article <960@Portia.Stanford.EDU>, joe@hanauma (Joe Dellinger) writes: > What would you expect the following program to print out? > #include > 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@europa.att.com