While Loops And Arrays Causing Very Odd Behaviour...maybe A Memory Mixup

- 1 answer

I'm tired of this tom-foolery occurring during runtime , although I'm sure we all are, when our programs screw up at runtime in the most obscure ways.

Getting to the point, the entire source code is a bit large to place here, but still <200 lines, so that's here . Use it if running the program, since the code I will post below is just functions, where I think the error lies.

Context : This is a sort of shift cipher with 8 different shifts taken using an 8 digit pin.

The issue is strange. Basically, the encrypt() function works correctly always -I've matched it by doing the algorithm for myself on paper ; for example, ABC is correctly encoded to 3c 45 46 -6f when the Pin is 12345678.

The strange issues are with the decrypt() function.

When the program is run for the first time, trying to run decrypt() on a valid ciphertext-pin pair always returns nothing except a /n (newline) . When tried with a different valid pin-ciphertext pair, after a successful run of encrypt() is done first, the decrypt() function just returns either the same message which was just encrypted or some other random output from the previously encoded message.

Without further ado, the legendarily screwed up decrypt function which I have rebuilt thrice now -

void decrypt()
    int *digits = pin(); int d[8];
    for (int i=0;i<8;i++)
        d[i] = *(digits + i); //puts each digit in a local array.

    printf("\nEnter encoded message -\n\n");
    int j; char ch, msg[3002];
    for(int i=0; i < 3000;i++)

            msg[i] = '\0'; //terminates string with \0
                ch = j - d[2];
            else if(fib(i)==1)
                ch = j + d[4];
            else if(luc(i)==1)
                ch = j - d[0];
            else if(pent(i)==1)
                ch = j + d[6];
            else if(hex(i)==1)
                ch = j - d[3];
            else if(prm(i)==1)
                ch = j + d[7];
            else {
                if(i%2 == 0)
                    ch = j - d[1];
                    ch = j + d[5];          
            msg[i] = ch;
    printf("\nDecrypted message -\n\n");

For context, as well as finding the culprits here, do make sure to read the full code here , with the pin() returning a pointer to a static int array holding all 8 digits , as well as the ctln() , fib(), luc(), pent(), hex(), prm() [ which check if position value i of char in message is a part of Catalan, Fibonacci , Lucas, Pentagon, Hexagon, Prime number series. More here.

Edit 1

I have already tried keeping different variable names, and some other things I can't fully recall. Also, because it is very relevant, below is the pin() function:

int *pin() 
    int num,q=0; static int pins[8];

    printf("Enter 8-digit PIN : ");
    scanf("%d", &num);

    for(register int i = 10000000 ; i >= 1 ; i = (i/10)) // i is position of digit.
        int d = ((num - (num % i)) / i); // d stores 'digit' ( divides quotient of (num % i) by i) 
        pins[q] = d; q++;
        num = (num - ( d * i )); 

    return pins ; // pointer to static array storing digits of PIN 

Edit 2

I had wrongly assigned pins[6] rather than pins[8] in the original code, I have corrected it but am still facing the same errors.

Edit 3

After correcting the mistake pointed out by MikeCAT, it now ignores the first character when deciphering.

Edit 4

The getchar() before scanf() was to blame, removing it fixes the last issue too. Thanks @MikeCAT !



In your decrypt() function, msg[i] = ch; is executed only if none of the functions ctln, fib, luc, pent, hex, prm returned 1.

Therefore, uninitialized value of non-static local variable msg, which is indeterminate, may be used for printing and undefined behavior may be invoked.

The part

            msg[i] = ch;

should be

            msg[i] = ch;

as it is done in encrypt() function.