Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #91396

Re: string swap

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: string swap
Date 2016-07-31 11:14 -0400
Organization A noiseless patient Spider
Message-ID <nnl4jp$gog$1@dont-email.me> (permalink)
References <6461d9d8-9101-47e4-a0a5-7609a32b8ec1@googlegroups.com>

Show all headers | View raw


On 07/31/2016 10:45 AM, sinbad wrote:
> why is the following piece of code crashing at "*(s + src) = *(s + dst)";
> I thought this code should swap the values at locations 0 and 2 and these
> indices are with in the bounds.
> 
> int
> swap(char *s, int src, int dst)
> {
>     int tmp;
> 
>     tmp = *(s + src);
>     *(s + src) = *(s + dst);
>     *(s + dst) = tmp;
>     return(0);

I address your actual problem at the bottom of this message.
The following comments have nothing to o with your problem, they're just
general programming advice:

The parentheses around the 0 in the return statement are unnecessary,
and give the incorrect impression that return() is a function call. It
isn't, and it will cause less confusion in the long run if you make a
habit of not using them.

Your function always returns 0, which means there's no reason to bother
checking what value it returns, which in turn means there's no reason
why the function should bother returning a value. You should have
declared it as a void function, and then you wouldn't even need a return
statement.

In this very limited context, you can be quite certain that swap() will
never be called with a null pointer. However, in the general case, you
can't be sure of that, so swap should check whether s is null. If it is,
swap should skip all of the other code in the function, and return an
error code - in which case returning an 'int' value is perfectly reasonable.

> }
> 
> 
> int
> main() {
>     char *s = "abc";
>     swap(s, 0, 2);
>     return(0);
> }

"In translation phase 7, a byte or code of value zero is appended to
each multibyte character sequence that results from a string literal or
literals.78) The multibyte character sequence is then used to initialize
an array of static storage duration and length just sufficient to
contain the sequence. ..." (6.4.5p6)

The string literal itself is automatically converted into a pointer to
the first element of that array (6.3.2.1p3), which is stored in s.

"... If the program attempts to modify such an array, the behavior is
undefined." (6.4.5p7)

swap(s, 0, 2) tries to modify both the first and third elements of that
array, which is your problem. What you need to do in order for this
program to work as you desire is to create a modifiable array:

    char sarr[] = "abc";

6.3.2.1p3 has a specific exemption for the case of a string literal
being used to initialize an array. Instead of being converted into a
pointer to char (which would be a constraint violation in this context),
the array is initialized by copying from the array corresponding to
"abc". Unlike "abc", the array sarr can safely be modified.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

string swap sinbad <sinbad.sinbad@gmail.com> - 2016-07-31 07:45 -0700
  Re: string swap Melzzzzz <mel@zzzzz.com> - 2016-07-31 16:51 +0200
    Re: string swap Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2016-07-31 12:48 -0400
  Re: string swap James Kuyper <jameskuyper@verizon.net> - 2016-07-31 11:14 -0400
    Re: string swap Real Troll <real.troll@trolls.com> - 2016-07-31 15:43 -0400
      Re: string swap "James R. Kuyper" <jameskuyper@verizon.net> - 2016-08-01 12:55 -0400
        Re: string swap "James R. Kuyper" <jameskuyper@verizon.net> - 2016-08-01 15:35 -0400
          Re: string swap supercat@casperkitty.com - 2016-08-01 13:09 -0700
            Re: string swap Keith Thompson <kst-u@mib.org> - 2016-08-01 14:36 -0700
            Re: string swap jameskuyper@verizon.net - 2016-08-01 15:58 -0700
  Re: string swap raltbos@xs4all.nl (Richard Bos) - 2016-07-31 15:38 +0000
    Re: string swap Tim Rentsch <txr@alumni.caltech.edu> - 2016-07-31 12:42 -0700
      Re: string swap Randy Howard <rhoward.mx@EverybodyUsesIt.com> - 2016-07-31 18:16 -0500
        Re: string swap Tim Rentsch <txr@alumni.caltech.edu> - 2016-08-01 07:33 -0700
      Re: string swap raltbos@xs4all.nl (Richard Bos) - 2016-08-03 13:21 +0000
        Re: string swap Tim Rentsch <txr@alumni.caltech.edu> - 2016-08-05 09:11 -0700
          Re: string swap raltbos@xs4all.nl (Richard Bos) - 2016-08-10 12:56 +0000
            Re: string swap Tim Rentsch <txr@alumni.caltech.edu> - 2016-08-17 09:45 -0700
              Re: string swap raltbos@xs4all.nl (Richard Bos) - 2016-08-21 12:42 +0000
  Re: string swap Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-07-31 17:16 +0100
  Re: string swap Les Cargill <lcargill99@comcast.com> - 2016-07-31 14:28 -0500

csiph-web