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