Initializing arrays of char

Bjorn Engsig bengsig at oracle.nl
Tue Oct 9 01:06:24 AEST 1990


Article <26860 at mimsy.umd.edu> by chris at mimsy.umd.edu (Chris Torek) says:
|
|  In Classic (K&R-1) C, a
|double-quoted string in an initializer context%, when setting the
|initial value of a character array, was treated uniformly as if it were
|a bracketed initializer consisting of all the characters, including
|the terminating NUL, in the string.
Yes, it seems to me that K&R1 says so - even if I would say it didn't.  The
rationale for ANSI C says that accepting 'char x[2] = "ab"' (omitting the NUL)
is due to widely existing practice.  This is in fact true, at least I have 
seen many Classic C compilers that allowed it and didn't warn about it.
Since K&R1 seems to be clear, how come the compilers accepted it?  Or
does K&R1 actually hide it somewhere?

As a comment to another note in this thread that string functions shouldn't be
used with non NUL terminated strings; strncpy is actually designed to work
with non NUL terminated fixed length strings, and you will normally use
'x[0]=0; strncat(x,s,n)' if you want a limited NUL terminated copy of strings,
whereas 'strncpy(x,s,n)' may yield surprises.
-- 
Bjorn Engsig,	       E-mail: bengsig at oracle.com, bengsig at oracle.nl
ORACLE Corporation   From IBM: auschs!ibmaus!cs.utexas.edu!uunet!oracle!bengsig

	"Stepping in others footsteps, doesn't bring you ahead"



More information about the Comp.lang.c mailing list