0

So i am making a small program to receive and decrypt a string, and then return the decrypted string and print it. I am quite new to C, so the whole pointer and string array thing still confuses me. My function: char **decrypt2 takes a string(char* pw) and a int(encryptionInt) to use for decryption. I manage to copy the string into another string, and change the value of it with the encryptionInt. But when i print the final array it contains some extra characters, and when i print the returned string in another function then the values look random and wrong.
Edit: Sorry, i realise that not all the arguments to the different functions are used.

char **decrypt2(char* pw, int encryptInt) {
    int pwLength = strlen(pw); 
    char arr1[pwLength]; 
    char *arr2[1];

    printf("pw length: %i\n", pwLength);
    for (int i = 0; i < pwLength; i++)
    {
        arr1[i] = pw[i];
        printf("standard: %c\n", arr1[i]); 
    }

    for (int i = 0; i < pwLength; i++)
    {
        arr1[i] = arr1[i] - encryptInt; //minus in ascii
        printf("decrypted: %c\n", arr1[i]);
    }

    arr2[0] = arr1;
    printf("decrypted final: %s\n", arr2[0]); 
    char **ptr = arr2;
    return ptr; 
}

int login(char* userName, char* loginPw, int encryptInt) {
   char **decryptedPWArray = decrypt2("test", encryptInt);
   printf("received string: %s\n", decryptedPWArray[0]); 
   return 0;
}

int main(int argc, char const *argv[])
{ 
   int true = login("anders", "test", 3);
}

So i expected the "decrypted final" and the "received string" output to be: qbpq, but this is what i get:
pw length: 4
standard: t
standard: e
standard: s
standard: t
decrypted: q
decrypted: b
decrypted: p
decrypted: q
decrypted final: qbpq@@
received string: F¿vìP@

Villain
  • 1
  • 1
  • 1
    For starters, `arr1` is not long enough to include the string terminating character (`\0`) – Federico klez Culloca Aug 25 '19 at 10:40
  • If you're just learning, temporarily apply this rule: functions can't return arrays. And you can't get around this by having them return pointers (or double pointers), either. So to write a function that decrypts a string, and "returns" the decrypted string, write the function so that you pass it *two* arrays, one of them an empty one to receive the decrypted output. Then, in the caller, allocate and pass that second, empty array, making sure it's big enough. (And then, in a month or two, do a web search on "How to return arrays in C", because there are other ways.) – Steve Summit Aug 25 '19 at 10:43
  • If you want to try to fix this program, the first things to look at are: (1) if `decrypt2` is supposed to return a simple string, that should be `char *`, not `char **`; (2) if `arr1` is supposed to contain the decrypted string that `decrypt2` returns, this will never work, because it's a local array, so it disappears when the function returns; and (3) you don't need `arr2`; it's adding nothing but confusion. – Steve Summit Aug 25 '19 at 10:55
  • The two main problems in the code as posted are that `arr1` is local and not big enough. – Steve Summit Aug 25 '19 at 11:00
  • @SteveSummit Thank you Steve, i will look into all the things you suggested! I will try to allocate arr1 to memory, and stop returning char arrays. – Villain Aug 25 '19 at 11:05

0 Answers0