Problem with the incrementation of pointer

H

HansWernerMarschke

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;Wheel is initialized like this:

This is one wheel:

char *wheel[18];

This is the first one:

wheel[0] = "EKMFLG DQVZNTOWYHXUSPAIBRCJ";

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.rotor[rotor_number].wheel[dest];
char *source = &the_enigma.rotor[rotor_number].wheel[src];
char *destination = &the_enigma.rotor[rotor_number].wheel[dest];
for (i = 0;i < the_enigma.chars-1; ++i)
{
*destination = *source; <--------------------- Segmentation
fault
source++;
destination++;
}
index = dest-1;
if (index < 0) index = 0;
the_enigma.rotor[rotor_number].wheel[index] = temp;
}

######### Thanks for help.
 
F

fred.l.kleinschmidt

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;Wheel is initialized like this:

This is one wheel:

char *wheel[18];

This is the first one:

wheel[0]  = "EKMFLG DQVZNTOWYHXUSPAIBRCJ";

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.rotor[rotor_number].wheel[dest];
   char *source = &the_enigma.rotor[rotor_number].wheel[src];
   char *destination = &the_enigma.rotor[rotor_number].wheel[dest];
   for (i = 0;i < the_enigma.chars-1; ++i)
   {
       *destination = *source;   <--------------------- Segmentation
fault
       source++;
       destination++;
   }
   index = dest-1;
   if (index < 0) index = 0;
   the_enigma.rotor[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.rotor[rotor_number].wheel;
for (i = 0;i < the_enigma.chars-1; ++i)
{
*destination = *source;
source++;
if ( source == wheel+the_enigma.chars ) {
source = wheel;
}
destination++;
if ( destination == wheel+the_enigma.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.
 
B

Ben Bacarisse

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;Wheel is initialized like this:

This is one wheel:

char *wheel[18];

This is the first one:

wheel[0] = "EKMFLG DQVZNTOWYHXUSPAIBRCJ";

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.rotor[rotor_number].wheel[dest];
char *source = &the_enigma.rotor[rotor_number].wheel[src];
char *destination = &the_enigma.rotor[rotor_number].wheel[dest];
for (i = 0;i < the_enigma.chars-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.rotor[rotor_number].wheel[0] if it is >
&the_enigma.rotor[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.rotor[rotor_number].wheel[index] = temp;
}

######### Thanks for help.
 
H

HansWernerMarschke

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.rotors-1); ++i)
{
// Find the char (The first pointer)
pchar = strchr(the_enigma.rotor.wheel,ch);
// Get the pointer to the actual rotor position (The second
pointer)
schar =
&the_enigma.rotor.wheel[the_enigma.rotor.position];
// Calculate the difference
shift = (int) (pchar - schar);
// Calculate the index on the succeeding wheel
index = (the_enigma.rotor[i+1].position + shift);
// If outside the range, correct
if (index < 0) index += the_enigma.chars;
if (index > the_enigma.chars-1) index -= the_enigma.chars;
// Substitute the char with the one from the succeeding wheel
ch = the_enigma.rotor[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
 
S

santosh

(e-mail address removed) wrote:

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:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top