An interesting behaviour in pri
mcdonald at uxe.cso.uiuc.edu
mcdonald at uxe.cso.uiuc.edu
Mon Mar 20 01:01:00 AEST 1989
In <960 at Portia.Stanford.EDU>, joe at 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.
More information about the Comp.lang.c
mailing list