[comp.lang.c] An interesting behaviour in pri

mcdonald@uxe.cso.uiuc.edu (03/19/89)

In <960@Portia.Stanford.EDU>, joe@hanauma (Joe Dellinger) asks:

> 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]);
>	}
>
> 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.