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


Groups > comp.lang.c > #91402

Re: string swap

From Lew Pitcher <lew.pitcher@digitalfreehold.ca>
Newsgroups comp.lang.c
Subject Re: string swap
Date 2016-07-31 12:48 -0400
Organization The Pitcher Digital Freehold
Message-ID <nnla52$4j6$1@dont-email.me> (permalink)
References <6461d9d8-9101-47e4-a0a5-7609a32b8ec1@googlegroups.com> <20160731165141.1127cdd3@maxa-pc>

Show all headers | View raw


On Sunday July 31 2016 10:51, in comp.lang.c, "Melzzzzz" <mel@zzzzz.com>
wrote:

> On Sun, 31 Jul 2016 07:45:44 -0700 (PDT)
> sinbad <sinbad.sinbad@gmail.com> 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);
>> }
>> 
>> 
>> int
>> main() {
>>     char *s = "abc";
>>     swap(s, 0, 2);
>>     return(0);
>> }
> 
> C is low level language. line `char* s = "abc";` does not allocates
> string...rather it assigns pointer to first element of non
> writeable string literal...

Sinbad:

To make this (and Richard Bos) point clearer, the C language does /not/
guarantee that the contents of string literal ( the thing pointed to by <<s>>
in {char *s = "abc";} ) can be modified. In fact, the standard specifically
states that an attempt to modify a string literal (by, for instance,
replacing one element value with another) invoked "undefined behaviour".

Such behaviour can include program termination, or aberrant or inconsistant
behaviour of the program logic.

This is important, because, from this pov,
  {
    char string_array[] = "abc";
    swap(string_array, 0, 2);
  }
should (modulo the correctness of the swap() function) perform as expected,
while
  {
    char *string_pointer = "abc";
    swap(string_pointer, 0, 2);
  }
or
  {
    swap("abc", 0, 2);
  }
likely will not.

In other words, even if you get swap() to work correctly, it can still fail,
even though the parameters seem to be "type correct" (string_array passes
into the function as "pointer to character", as does string_pointer).

-- 
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

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