Stack Exchange
Code working fine in vscode of windows but giving error in vscode of Ubuntu
I am trying to build a basic Ceaser cipher of cryptography. The code which I have written is working in vscode of Windows but the exact same code is giving me error in vscode of Ubuntu.
More particularly I am getting Segmentation fault error when I try to run the code in vscode of Ubuntu OS.
Code :
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char * Encrypted(char ** input, int key){
    printf("%s\n", *input);
    int length = strlen(*input);
    printf("Length is %d ", length);
    char * curr = *input;
    char * encrypt = malloc(length);
    strcpy(encrypt, "");
    for(int i=0; i < length; i++){
        int current = (int)curr[i];
        char currentDecrypted = (char)(65+(current + key - 65)%26);
        strncat(encrypt, &currentDecrypted, 1);
    }
    return encrypt;
}


int main(void){
    char * input;
    printf("Enter the text to be encrypted : ");
    gets(input);
    // puts(input);
    int key;
    printf("Enter the key : ");
    scanf("%d", &key);
    printf("The encrypted text is : %s\n", Encrypted(&input, key));
}

Where exactly am I making a mistake.?
Thanks in advance for the solution.

you havent allocated memory for input and not enough memory for zero end for encrypt

Sir, if it is so then why is it executing fine in vscode of Windows?

@Valiveti Swamy Naga Sai Nivas Just do not use Ubuntu.:)

@ValivetiSwamyNagaSaiNivas Well, there are sort of two possibilities here. (1) Your code is broken, as explained by Iłya Bursov, and that's why it doesn't work on Ubuntu. (2) Ubuntu is broken, and that's why your code doesn't work there, despite being fine, as suggested by the fact that it "works" under Windows. But! The chance that Ubuntu is broken (and for such a very, very simple program) is quite low.  Therefore, we really must consider possibility 1.  And in fact: In programming in general, but in C especially: it is quite possible for a program to (seem to) work for the wrong reasons!

If some code works fine on one machine or OS and does not work on another is a really good indicator that you are invoking undefined behaviour. And with the very basic nature of undefined behaviour it is also possible to fool you and seemingly work while still being broken. It does not need to show some error to you. It may silently fail where you don't see it.

@ValivetiSwamyNagaSaiNivas "Why is it executing fine?"  See the answer to this very similar previous question.

@ValivetiSwamyNagaSaiNivas it looks like working only because it is undefined behaviour

@ValivetiSwamyNagaSaiNivas The point is that (a) your code looks like it's written to handle capital letters, but not lower-case letters, and (b) it's easier all around if you use constants like 'A' and 'a', rather than magic numbers like 65 and 97.

@ValivetiSwamyNagaSaiNivas Pay attention to that as the variable key has the type int then the user of the program can enter a negative value for key.

The statement:
char * input;

declares a pointer to char, or a char *, but does not allocate any memory for it. The pointer is indeterminate, and dereferencing an indeterminate pointer invokes undefined behaviour.
From C11:

The behaviour is undefined in the following circumstances:
....

The value of an object with automatic storage duration is used while
it is indeterminate (6.2.4, 6.7.9, 6.8)


Then, the calls to gets and printf try to access memory they have no business accessing, and the call to strlen tries to determine the length of a string in unallocated memory.
Aside:
gets(input)

gets does no error-checking, is inherently dangerous and shouldn't be used. It has been removed from the C standard. Consider using fgets instead.

"determine the length of an indeterminate pointer" I think you mean the length of a string in unallocated memory. Pointers always have determinate size.
