0

I am trying to get input from the console with this code but it gives me runtime exception at some memory location each time I try to run it and enter the first possible input. I am using Visual Studio 2010. I got same problems with MingW and Dev C++. However, the code ran fine with the old TurboC3 compiler.

int Nowhere(int x);
...
char* AtBashEncrypt(char* message);
char* AtBashDecrypt(char* encrypted);

int main()
{
    char *input = "", *ciphertext = "", *plaintext = "";
    system("cls");
    printf("AtBash Cipher\nEnter a string to be encrypted: ");
    gets(input); //this is where I get the error
    ciphertext = AtBashEncrypt(input);
    ...
    getch();
}

What could possibly be wrong with it?

4

1 回答 1

1
 char *input = "";

是指向驻留在只读内存中的字符串文字的指针,您无法修改其内容。任何这样做的尝试都会导致未定义的行为。你需要的是一个数组。

#define MAX_SIZE 256
char input[MAX_SIZE]="";

好读:
char a[] = ?string?; 之间有什么区别?和 char *p = ?string?;?

此外,您应该使用 fgets 而不是 gets

于 2013-03-08T03:56:54.617 回答