home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!caen!hellgate.utah.edu!lanl!cochiti.lanl.gov!jlg
- From: jlg@cochiti.lanl.gov (J. Giles)
- Subject: Re: No, but strncat() is stupid (was: definition of strncpy stupid?)
- Message-ID: <1992Dec14.190936.1589@newshost.lanl.gov>
- Sender: news@newshost.lanl.gov
- Organization: Los Alamos National Laboratory
- References: <DAVIS.92Dec8113707@pacific.mps.ohio-state.edu> <1992Dec10.175103.11502@newshost.lanl.gov> <1992Dec11.095325.1871@falcon.aamrl.wpafb.af.mil>
- Date: Mon, 14 Dec 1992 19:09:36 GMT
- Lines: 42
-
- In article <1992Dec11.095325.1871@falcon.aamrl.wpafb.af.mil>, bkottmann@falcon.aamrl.wpafb.af.mil (Brett Kottmann) writes:
- |> In article <1992Dec10.175103.11502@newshost.lanl.gov>, jlg@cochiti.lanl.gov (J. Giles) writes:
- |> > I can't simply say
- |> >
- |> > strncat(s1,s2,N);
- |> >
- |> > This would *add* N characters to s1, not limit it's total length
- |> > to N. In order to do what I want, I have to:
- |> >
- |> > strncat(s1, s2, N - strlen(s1));
- |> >
- |> > Except, I can't do this either. Previous operations on s1 may
- |> > have left it without null termination - so strlen() won't work.
- |>
- |> It better have null termination. strncat uses that to start storing
- |> s2.
-
- You've missed my point. If the length of `s1' is equal to `N', there is
- no room for null termination. Therefore `s1' will not have it. No matter,
- the amount I want strncat to move in that case it zero characters. So,
- strncat() will not have to find any null termination of `s1'. I was
- pointing out the same thing you were - only one step earlier. Since
- `s1' may not be null terminated (it can't be if there's no room), then
- strlen() won't work on it. Of course, you could pretend the allocated
- length was one character shorter than it actually was and insure that
- there was always room for a terminating null. That still doesn't solve
- the problem that you have to do all the calculations yourself in order
- to make sure that you don't run past the end : you still have to do the
- redundant `N-strlen(s1)' expression.
-
- Personally, I think that null termination is a bad idea anyway, and
- when I have to do character manipulation in C, I use the mem***()
- functions exclusively. In fact, all the character variables I
- declare are structs which explicitly carry the present length, the
- allocated length, and the pointer to the actual string. I then use
- string manipulation functions of my own (which use the mem***() stuff)
- that automatically maintain the information in the struct. This is
- almost *always* faster than the str***() or the strn***() functions
- and is certainly safer.
-
- --
- J. Giles
-