comp.lang.ada
 help / color / mirror / Atom feed
From: "David Thompson" <david.thompson1@worldnet.att.net>
Subject: Re: How to delete charcters in a string?
Date: Mon, 18 Feb 2002 03:48:19 GMT
Date: 2002-02-18T03:48:19+00:00	[thread overview]
Message-ID: <7Q_b8.8417$BR3.454358@bgtnsc04-news.ops.worldnet.att.net> (raw)
In-Reply-To: uy9idcclz.fsf@wanadoo.fr

Pascal Obry <p.obry@wanadoo.fr> wrote :
>
> Stephen Leake <stephen.a.leake.1@gsfc.nasa.gov> 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 <string.h> 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






  reply	other threads:[~2002-02-18  3:48 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-01-30  0:25 How to delete charcters in a string? Anthony Wise
2002-01-30  2:44 ` Steve Doiel
2002-01-30  3:22 ` dale
2002-01-30  6:37   ` tmoran
2002-01-30  7:49     ` Dale Stanbrough
2002-01-30 15:04       ` Marin David Condic
2002-01-30 11:21 ` Preben Randhol
2002-01-30 15:15   ` Marin David Condic
2002-01-31 14:47 ` Ted Dennison
2002-01-31 16:08   ` Larry Hazel
2002-02-01 15:59     ` Ted Dennison
2002-02-01 17:05     ` Darren New
2002-02-01  6:14   ` tmoran
2002-02-01 15:56     ` Stephen Leake
2002-02-01 17:53       ` Pascal Obry
2002-02-18  3:48         ` David Thompson [this message]
2002-02-18 19:44           ` Stephen Leake
     [not found] <F207EAsx2Pwf500RR1h00019d09@hotmail.com>
2002-01-30  1:16 ` Eric Merritt
2002-01-30  1:16 ` Eric Merritt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox