Problem with the incrementation of pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • HansWernerMarschke@web.de

    Problem with the incrementation of pointer

    I hate pointers. I want to copy something from one memory position to
    another memory position. Both strings overlap.

    // The rotor type
    typedef struct
    {
    int wheel_number;
    char *wheel;
    char start; // Startposition
    int stepsize;
    int position;
    } rotor_type;

    // The enigma type
    typedef struct
    {
    int chars;
    int rotors;
    rotor_type *rotor;
    } enigma_type;
    // This is the only global variable
    enigma_type the_enigma;Whee l is initialized like this:

    This is one wheel:

    char *wheel[18];

    This is the first one:

    wheel[0] = "EKMFLG DQVZNTOWYHXUSPA IBRCJ";

    And now rotate the wheel from source to destination.
    Left to right or right to left.
    src and dest are the positions from 0..26 (26 chars plus space).

    // Rotate the wheel in both directions
    void rotate_wheel (int rotor_number, int dest, int src)
    {
    int i;
    int index;
    // The rotation is done by shifting the chars in the array
    char temp = the_enigma.roto r[rotor_number].wheel[dest];
    char *source = &the_enigma.rot or[rotor_number].wheel[src];
    char *destination = &the_enigma.rot or[rotor_number].wheel[dest];
    for (i = 0;i < the_enigma.char s-1; ++i)
    {
    *destination = *source; <--------------------- Segmentation
    fault
    source++;
    destination++;
    }
    index = dest-1;
    if (index < 0) index = 0;
    the_enigma.roto r[rotor_number].wheel[index] = temp;
    }

    ######### Thanks for help.
  • fred.l.kleinschmidt@boeing.com

    #2
    Re: Problem with the incrementation of pointer

    On Apr 25, 6:58 am, HansWernerMarsc ...@web.de wrote:
    I hate pointers. I want to copy something from one memory position to
    another memory position. Both strings overlap.
    >
    // The rotor type
    typedef struct
    {
        int  wheel_number;
        char *wheel;
        char start; // Startposition
        int stepsize;
        int position;
    >
    } rotor_type;
    >
    // The enigma type
    typedef struct
    {
        int chars;
        int rotors;
        rotor_type *rotor;} enigma_type;
    >
    // This is the only global variable
    enigma_type the_enigma;Whee l is initialized like this:
    >
    This is one wheel:
    >
    char *wheel[18];
    >
    This is the first one:
    >
    wheel[0]  = "EKMFLG DQVZNTOWYHXUSPA IBRCJ";
    >
    And now rotate the wheel from source to destination.
    Left to right or right to left.
    src and dest are the positions from 0..26 (26 chars plus space).
    >
    // Rotate the wheel in both directions
    void rotate_wheel (int rotor_number, int dest, int src)
    {
       int i;
       int index;
       // The rotation is done by shifting the chars in the array
       char temp = the_enigma.roto r[rotor_number].wheel[dest];
       char *source = &the_enigma.rot or[rotor_number].wheel[src];
       char *destination = &the_enigma.rot or[rotor_number].wheel[dest];
       for (i = 0;i < the_enigma.char s-1; ++i)
       {
           *destination = *source;   <--------------------- Segmentation
    fault
           source++;
           destination++;
       }
       index = dest-1;
       if (index < 0) index = 0;
       the_enigma.roto r[rotor_number].wheel[index] = temp;
    >
    }
    >
    ######### Thanks for help.
    You have to worry about 'destination' or 'source' pointing
    past the end of the array.

    char *wheel = the_enigma.roto r[rotor_number].wheel;
    for (i = 0;i < the_enigma.char s-1; ++i)
    {
    *destination = *source;
    source++;
    if ( source == wheel+the_enigm a.chars ) {
    source = wheel;
    }
    destination++;
    if ( destination == wheel+the_enigm a.chars ) {
    destination = wheel;
    }
    }
    This will keepo you from getting a sxeg fault, but
    your algorithm still does not work properly.

    It is much easier to use a second array, copying from
    source array to destination array, then swapping the
    array pointers.
    --
    Fred Kleinschmdit

    Comment

    • Ben Bacarisse

      #3
      Re: Problem with the incrementation of pointer

      HansWernerMarsc hke@web.de writes:
      I hate pointers. I want to copy something from one memory position to
      another memory position. Both strings overlap.
      >
      // The rotor type
      typedef struct
      {
      int wheel_number;
      char *wheel;
      char start; // Startposition
      int stepsize;
      int position;
      } rotor_type;
      >
      // The enigma type
      typedef struct
      {
      int chars;
      int rotors;
      rotor_type *rotor;
      } enigma_type;
      // This is the only global variable
      enigma_type the_enigma;Whee l is initialized like this:
      >
      This is one wheel:
      >
      char *wheel[18];
      >
      This is the first one:
      >
      wheel[0] = "EKMFLG DQVZNTOWYHXUSPA IBRCJ";
      >
      And now rotate the wheel from source to destination.
      Left to right or right to left.
      src and dest are the positions from 0..26 (26 chars plus space).
      >
      // Rotate the wheel in both directions
      void rotate_wheel (int rotor_number, int dest, int src)
      {
      int i;
      int index;
      // The rotation is done by shifting the chars in the array
      char temp = the_enigma.roto r[rotor_number].wheel[dest];
      char *source = &the_enigma.rot or[rotor_number].wheel[src];
      char *destination = &the_enigma.rot or[rotor_number].wheel[dest];
      for (i = 0;i < the_enigma.char s-1; ++i)
      {
      *destination = *source; <--------------------- Segmentation
      fault
      Unless src and dest are both 0, you will eventually "fall off" the end
      of both arrays.
      source++;
      destination++;
      A messy fix is to "wrap" both pointers here (you set source back to
      &the_enigma.rot or[rotor_number].wheel[0] if it is >
      &the_enigma.rot or[rotor_number].wheel[26] and the same for
      destination). Another way is to use the index i and "wrap" only that.

      Better yet, don't rotate at all (since it is a frequent operation).
      Just store the "offset" of each rotor and add that when you do a
      lookup. Rotating is the just adding or subtracting from the offset.
      The lookup becomes rotor[rotor_number].wheel[(idx + offset) % 27].

      Is this coursework? If so, I have said too much already...
      }
      index = dest-1;
      if (index < 0) index = 0;
      the_enigma.roto r[rotor_number].wheel[index] = temp;
      }
      >
      ######### Thanks for help.
      --
      Ben.

      Comment

      • HansWernerMarschke@web.de

        #4
        Re: Problem with the incrementation of pointer

        First of all. This is not coursework. I try to develop some examples
        for the programming of MATLAB/SIMULINK in C and/or C++.
        And I have not done programming in C or C++ for a long time. But
        MATLAB only supports C, C++, Ada and Fortran.
        So, now it works. I have changed some things but is this the shortest
        way to do this ?
        So does it looks now.

        char encrypt_char (char ch)
        {
        int i;
        int shift, index;
        char *pchar, *schar;
        // Rotate the wheels
        rotate ();
        // Do the encryption
        for (i = 0; i < (the_enigma.rot ors-1); ++i)
        {
        // Find the char (The first pointer)
        pchar = strchr(the_enig ma.rotor[i].wheel,ch);
        // Get the pointer to the actual rotor position (The second
        pointer)
        schar =
        &the_enigma.rot or[i].wheel[the_enigma.roto r[i].position];
        // Calculate the difference
        shift = (int) (pchar - schar);
        // Calculate the index on the succeeding wheel
        index = (the_enigma.rot or[i+1].position + shift);
        // If outside the range, correct
        if (index < 0) index += the_enigma.char s;
        if (index the_enigma.char s-1) index -= the_enigma.char s;
        // Substitute the char with the one from the succeeding wheel
        ch = the_enigma.roto r[i+1].wheel[index];
        }
        return ch;
        }

        Two more question as addendum:

        Is there a bit rotation in C (Bitshift is <<) ?
        Are there commands for permutations like permute an array, calculate
        all possible permutations ?

        ############### ### Thanks again

        Comment

        • santosh

          #5
          Re: Problem with the incrementation of pointer

          HansWernerMarsc hke@web.de wrote:

          <snip>
          Is there a bit rotation in C (Bitshift is <<) ?
          No built in support. However it can be done using the existing bit
          operations.

          Here are a couple of solutions for right rotate:

          <http://clc-wiki.net/wiki/KR2_Exercise_2-08>
          Are there commands for permutations like permute an array, calculate
          all possible permutations ?
          Again no. You need to do so manually. Just recently we had a thread
          about permutations in which some code was posted. In fact a Google
          search of comp.lang.c will likely turn up lots of code for bit rotate
          and string permutations, as they are repeatedly asked questions.

          Comment

          Working...