stupid compilers

mccaugh at sunb0.cs.uiuc.edu mccaugh at sunb0.cs.uiuc.edu
Sun Sep 2 15:47:00 AEST 1990


Hmmmmm: "pure luck" that the first program works, comments the previous note.
But the question orignally posed did not address correctness of either program,
but rather why the second version precipitates a segmentation fault where the
first one does not. So what is the key difference in the two programs? It would
appear to be in the declaration of variable 'line' which is a null (length = 0)
string in the first declaration (char []) and a char-ptr in the second. We are
not informed as to whether the assignment (via 'strcpy') caused the problem or
the subsequent 'printf' but I would suspect the latter. (If the former caused
the problem in the second version, why not in the first?) Perhaps "%s" allows
for "normal" execution in the first program -- since 'line' is technically a
string -- but not in the second. I, too, have encountered a similar problem
with C compilers on VAXen.  My point is that certain compilers MAY draw some
serious distinction between char-ptrs and "true" strings (char [*]) even when
the string is null.

   Since un-initialized ptrs so often lead to segmentation faults, here is my
guess as to what happened. The first declaration (char line [];) must have
initialized variable 'line' as a char-ptr to some 0-length area, while the
second declaration (char *line;) left 'line' un-initialized. Hence the value
of 'line' in the first case was legitimate -- even if it addressed 0-length
space -- while the un-initialized "value" of 'line' in the second case could 
not even be considered legitimate.

 Scott McCaughrin



More information about the Comp.lang.c mailing list