String reversing problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith Thompson

    #46
    Re: String reversing problem

    "tmp123" <tmp123@menta.n et> writes:[color=blue]
    > Emmanuel Delahaye wrote:[color=green]
    >> tmp123 a écrit :[/color][/color]
    [...][color=blue][color=green][color=darkred]
    >> > or to add more
    >> > variables to it. Sometimes only modify code is allowed (i.e: patching
    >> > firmware in real time systems without stop them).[/color]
    >>
    >> Sounds to be a twisted way of thinking...[/color]
    >
    > No, it sounds like a real situation. Imagine you have a lot of machines
    > controling some public service of your country. Imagine these machines
    > have one process with lots of threads, each one controlling one user
    > session. Now, ops, you find an error on code: two variables must be
    > swap.
    >
    > What you do? Stop all country? Update a few code is easy, but better
    > not to change in a live system the stack structure. Thus, use a trick
    > like a^=b^... could be the difference between a big problem and a
    > critical problem.[/color]

    I can imagine the need to patch live code under some circumstances,
    but doing so is not C programming.

    It's conceivable, I suppose, that in the process of doing so you might
    need to use the xor trick to swap two variables, but it hardly seems
    likely.
    [color=blue]
    > Moreover, in the previous post there was a lot of person saying "to
    > swap you need a local variable". Well, like it is explained in the
    > first level of any good programming course, there are more options.[/color]

    Sure, there are more options, but no better ones. The dangers of the
    xor trick are explained in the C FAQ. Some CPUs may have swap
    instructions, but there's no direct way to use them from C (though a
    decent optimizing compiler should be able to generate one from the
    obvious code).

    The way to swap two variables in C is:
    temp = x;
    x = y;
    y = x;
    If you feel the need to use some other method, you should explain why
    the obvious technique won't work for you.

    [...][color=blue][color=green]
    >> So what ? It seems that you are having hard time to find a real failiure
    >> in Christian's code. Never mind, making a fool of yourself in public was
    >> definitely your choice.[/color]
    >
    > But never ignorant.[/color]

    Never? I find that difficult to believe. My own areas of ignorance
    are vast (though I try to acknowledge them); I would expect the same
    of any finite being.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.

    Comment

    • pete

      #47
      Re: String reversing problem

      Albert wrote:
      [color=blue]
      > char s[5];
      >
      > s[0] = 'h';
      > s[1] = 'e';
      > s[2] = 'l';
      > s[3] = 'l';
      > s[4] = 'o';[/color]

      That's not a string.

      /* BEGIN new.c */

      #include <stdio.h>
      #include <string.h>

      char *str_rev(char *s);

      int main(void)
      {
      char s[] = "hello";

      puts(str_rev(s) );
      return 0;
      }

      char *str_rev(char *s)
      {
      char *t, swap;
      char *const p = s;

      if (*s != '\0') {
      t = s + strlen(s + 1);
      while (t > s) {
      swap = *t;
      *t-- = *s;
      *s++ = swap;
      }
      }
      return p;
      }

      /* END new.c */


      --
      pete

      Comment

      Working...