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


Groups > comp.lang.c > #91395

Re: string swap

From Melzzzzz <mel@zzzzz.com>
Newsgroups comp.lang.c
Subject Re: string swap
Date 2016-07-31 16:51 +0200
Organization albasani.net
Message-ID <20160731165141.1127cdd3@maxa-pc> (permalink)
References <6461d9d8-9101-47e4-a0a5-7609a32b8ec1@googlegroups.com>

Show all headers | View raw


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...

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