From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,decb0c44a2cbd5dc X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-02-17 19:48:20 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!arclight.uoregon.edu!wn4feed!worldnet.att.net!135.173.83.71!wnfilter1!worldnet-localpost!bgtnsc04-news.ops.worldnet.att.net.POSTED!not-for-mail From: "David Thompson" Newsgroups: comp.lang.ada References: <4519e058.0201310647.3637d7a9@posting.google.com> Subject: Re: How to delete charcters in a string? X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2615.200 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 Message-ID: <7Q_b8.8417$BR3.454358@bgtnsc04-news.ops.worldnet.att.net> Date: Mon, 18 Feb 2002 03:48:19 GMT NNTP-Posting-Host: 12.89.131.81 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc04-news.ops.worldnet.att.net 1014004099 12.89.131.81 (Mon, 18 Feb 2002 03:48:19 GMT) NNTP-Posting-Date: Mon, 18 Feb 2002 03:48:19 GMT Organization: AT&T Worldnet Xref: archiver1.google.com comp.lang.ada:20100 Date: 2002-02-18T03:48:19+00:00 List-Id: Pascal Obry wrote : > > Stephen Leake writes: > > > tmoran@acm.org writes: ... > > > How would you delete a character from a C string? Seems to me that's > > > probably harder that with an Ada string (lack of slice assignment), > > > > char *S1 = "ABCDEF"; > > > > strncpy (S1 + 3, S1 + 4, 3); > > > > result in *S1 is "ABDEF". > > No it's not. All C arrays, including strings, are zero-origin, so _if_ this worked it would produce ABCEF. But: 1) you can't legally modify (store to) a string literal value. _Some_ implementations put them in readonly storage and this will fault. _Some_ implementations share storage for identical(ly-suffixed) values, so modifying one changes the other(s) -- like the classic FORTRAN (II? IV?) bug of changing say the constant 1 to be 2. In C++ string literal values are at least qualified const, which couldn't be done in Standard C for compatibility with the pre-existing code base, so _some_ (simple) violations of this rule are caught by the compiler, but even in C++ there is a legal though nonpreferred conversion to non-const char pointer. Note char foo[] = "ABCDEF" is different. That allocates space and _initializes_ it by copying the literal value -- at least notionally, for static-duration variables the copy is often embedded in process creation -- and it can then be legally modified; but see next. 2) all the routines have Undefined Behavior (in Ada terms, are erroneous) for overlapping buffers, with the single exception of memmove(). This means in theory _anything_ is permissible; in practice, the likely problem is that fetches and/or stores are reordered and you do get a deterministic value, just the wrong one. > > No where near as pretty or readable as Ada, but just as short and > > easy. And I've probably got the "src" and "dest" parameters swapped. > > Man, I hate C :). > Actually (dest,src,len) is the only part you got right. I decline to guess what if anything this signifies. > And that is not what you wanted too :) > > result in *S1 is "ABDEFF". > That isn't right either. See above. -- - David.Thompson 1 now at worldnet.att.net