Path: utzoo!utgpu!utstat!jarvis.csri.toronto.edu!mailrus!ames!ncar!tank!uxc!uxc.cso.uiuc.edu!uxd.cso.uiuc.edu!uxe.cso.uiuc.edu!mcdonald From: mcdonald@uxe.cso.uiuc.edu Newsgroups: comp.lang.c Subject: Re: An interesting behaviour in pri Message-ID: <225800144@uxe.cso.uiuc.edu> Date: 19 Mar 89 15:01:00 GMT References: <15938@cup.portal.com> Lines: 27 Nf-ID: #R:cup.portal.com:15938:uxe.cso.uiuc.edu:225800144:000:984 Nf-From: uxe.cso.uiuc.edu!mcdonald Mar 19 09:01:00 1989 In <960@Portia.Stanford.EDU>, joe@hanauma (Joe Dellinger) asks: > 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]); > } > > Just "\n", right? On our system it prints out "(null)\n"!!! No, I would expect one of three things: print out (null) as it did for you, print out something like "fatal err: reference to memory outside process", or print out lots of garbage. You are passing an "int" where it is expecting a char pointer. If sizeof(int) == sizeof(char *), you are likely to get (null) or some other indication that you passed it a null pointer. If sizeof(int) != sizeof(char *), which is the more general case, one of the others would happen. If printf were not specifically known to take a variable number (and type) of argument, a fourth possibility might be an even more flaming end to the execution of the program due to stack corruption.